HexTransformationsPlugin.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Abstract class for the hex transformations plugins
  5. *
  6. * @package PhpMyAdmin-Transformations
  7. * @subpackage Hex
  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 hex transformations plugins.
  15. *
  16. * @package PhpMyAdmin
  17. */
  18. abstract class HexTransformationsPlugin 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 hexadecimal representation of data. Optional first'
  29. . ' parameter specifies how often space will be added (defaults'
  30. . ' to 2 nibbles).'
  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 stdClass|null $meta meta information
  39. *
  40. * @return string
  41. */
  42. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  43. {
  44. // possibly use a global transform and feed it with special options
  45. $cfg = $GLOBALS['cfg'];
  46. $options = $this->getOptions($options, $cfg['DefaultTransformations']['Hex']);
  47. $options[0] = intval($options[0]);
  48. if ($options[0] < 1) {
  49. return bin2hex($buffer);
  50. } else {
  51. return chunk_split(bin2hex($buffer), $options[0], ' ');
  52. }
  53. }
  54. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  55. /**
  56. * Gets the transformation name of the specific plugin
  57. *
  58. * @return string
  59. */
  60. public static function getName()
  61. {
  62. return "Hex";
  63. }
  64. }