HexTransformationsPlugin.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Abstract class for the hex 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 bin2hex;
  10. use function chunk_split;
  11. use function intval;
  12. /**
  13. * Provides common methods for all of the hex transformations plugins.
  14. */
  15. abstract class HexTransformationsPlugin 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 hexadecimal representation of data. Optional first'
  26. . ' parameter specifies how often space will be added (defaults'
  27. . ' to 2 nibbles).'
  28. );
  29. }
  30. /**
  31. * Does the actual work of each specific transformations plugin.
  32. *
  33. * @param string $buffer text to be transformed
  34. * @param array $options transformation options
  35. * @param stdClass|null $meta meta information
  36. *
  37. * @return string
  38. */
  39. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  40. {
  41. // possibly use a global transform and feed it with special options
  42. $cfg = $GLOBALS['cfg'];
  43. $options = $this->getOptions($options, $cfg['DefaultTransformations']['Hex']);
  44. $options[0] = intval($options[0]);
  45. if ($options[0] < 1) {
  46. return bin2hex($buffer);
  47. }
  48. return chunk_split(bin2hex($buffer), $options[0], ' ');
  49. }
  50. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  51. /**
  52. * Gets the transformation name of the specific plugin
  53. *
  54. * @return string
  55. */
  56. public static function getName()
  57. {
  58. return 'Hex';
  59. }
  60. }