SubstringTransformationsPlugin.php 2.6 KB

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