SubstringTransformationsPlugin.php 2.6 KB

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