SubstringTransformationsPlugin.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  10. use PhpMyAdmin\Plugins\TransformationsPlugin;
  11. /**
  12. * Provides common methods for all of the substring transformations plugins.
  13. *
  14. * @package PhpMyAdmin
  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 string $meta meta information
  39. *
  40. * @return string
  41. */
  42. public function applyTransformation($buffer, array $options = array(), $meta = '')
  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. if ($options[1] != 'all') {
  49. $newtext = mb_substr(
  50. $buffer,
  51. $options[0],
  52. $options[1]
  53. );
  54. } else {
  55. $newtext = mb_substr($buffer, $options[0]);
  56. }
  57. $length = mb_strlen($newtext);
  58. $baselength = mb_strlen($buffer);
  59. if ($length != $baselength) {
  60. if ($options[0] != 0) {
  61. $newtext = $options[2] . $newtext;
  62. }
  63. if (($length + $options[0]) != $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. }