SubstringTransformationsPlugin.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Abstract class for the substring transformations plugins
  5. *
  6. * @package PhpMyAdmin-Transformations
  7. * @subpackage Substring
  8. */
  9. declare(strict_types=1);
  10. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  11. use PhpMyAdmin\Plugins\TransformationsPlugin;
  12. use stdClass;
  13. /**
  14. * Provides common methods for all of the substring transformations plugins.
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. abstract class SubstringTransformationsPlugin extends TransformationsPlugin
  19. {
  20. /**
  21. * Gets the transformation description of the specific plugin
  22. *
  23. * @return string
  24. */
  25. public static function getInfo()
  26. {
  27. return __(
  28. 'Displays a part of a string. The first option is the number of'
  29. . ' characters to skip from the beginning of the string (Default 0).'
  30. . ' The second option is the number of characters to return (Default:'
  31. . ' until end of string). The third option is the string to append'
  32. . ' and/or prepend when truncation occurs (Default: "…").'
  33. );
  34. }
  35. /**
  36. * Does the actual work of each specific transformations plugin.
  37. *
  38. * @param string $buffer text to be transformed
  39. * @param array $options transformation options
  40. * @param stdClass|null $meta meta information
  41. *
  42. * @return string
  43. */
  44. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  45. {
  46. // possibly use a global transform and feed it with special options
  47. // further operations on $buffer using the $options[] array.
  48. $cfg = $GLOBALS['cfg'];
  49. $options = $this->getOptions($options, $cfg['DefaultTransformations']['Substring']);
  50. if ($options[1] != 'all') {
  51. $newtext = mb_substr(
  52. $buffer,
  53. $options[0],
  54. $options[1]
  55. );
  56. } else {
  57. $newtext = mb_substr($buffer, $options[0]);
  58. }
  59. $length = mb_strlen($newtext);
  60. $baselength = mb_strlen($buffer);
  61. if ($length != $baselength) {
  62. if ($options[0] != 0) {
  63. $newtext = $options[2] . $newtext;
  64. }
  65. if (($length + (int) $options[0]) != $baselength) {
  66. $newtext .= $options[2];
  67. }
  68. }
  69. return htmlspecialchars($newtext);
  70. }
  71. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  72. /**
  73. * Gets the transformation name of the specific plugin
  74. *
  75. * @return string
  76. */
  77. public static function getName()
  78. {
  79. return "Substring";
  80. }
  81. }