RegexValidationTransformationsPlugin.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Abstract class for the regex validation input transformations plugins
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  7. use PhpMyAdmin\Plugins\IOTransformationsPlugin;
  8. use stdClass;
  9. use function htmlspecialchars;
  10. use function preg_match;
  11. use function sprintf;
  12. /**
  13. * Provides common methods for all of the regex validation
  14. * input transformations plugins.
  15. */
  16. abstract class RegexValidationTransformationsPlugin extends IOTransformationsPlugin
  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. 'Validates the string using regular expression '
  27. . 'and performs insert only if string matches it. '
  28. . 'The first option is the Regular Expression.'
  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 stdClass|null $meta meta information
  37. *
  38. * @return string
  39. */
  40. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  41. {
  42. // reset properties of object
  43. $this->reset();
  44. if (! empty($options[0]) && ! preg_match($options[0], $buffer)) {
  45. $this->success = false;
  46. $this->error = sprintf(
  47. __('Validation failed for the input string %s.'),
  48. htmlspecialchars($buffer)
  49. );
  50. }
  51. return $buffer;
  52. }
  53. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  54. /**
  55. * Gets the transformation name of the specific plugin
  56. *
  57. * @return string
  58. */
  59. public static function getName()
  60. {
  61. return 'Regex Validation';
  62. }
  63. }