HexTransformationsPlugin.php 1.8 KB

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