RegexValidationTransformationsPlugin.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\FieldMetadata;
  8. use PhpMyAdmin\Plugins\IOTransformationsPlugin;
  9. use function __;
  10. use function htmlspecialchars;
  11. use function preg_match;
  12. use function sprintf;
  13. /**
  14. * Provides common methods for all of the regex validation
  15. * input transformations plugins.
  16. */
  17. abstract class RegexValidationTransformationsPlugin extends IOTransformationsPlugin
  18. {
  19. /**
  20. * Gets the transformation description of the specific plugin
  21. *
  22. * @return string
  23. */
  24. public static function getInfo()
  25. {
  26. return __(
  27. 'Validates the string using regular expression '
  28. . 'and performs insert only if string matches it. '
  29. . 'The first option is the Regular Expression.'
  30. );
  31. }
  32. /**
  33. * Does the actual work of each specific transformations plugin.
  34. *
  35. * @param string $buffer text to be transformed
  36. * @param array $options transformation options
  37. * @param FieldMetadata|null $meta meta information
  38. *
  39. * @return string
  40. */
  41. public function applyTransformation($buffer, array $options = [], ?FieldMetadata $meta = null)
  42. {
  43. // reset properties of object
  44. $this->reset();
  45. if (! empty($options[0]) && ! preg_match($options[0], $buffer)) {
  46. $this->success = false;
  47. $this->error = sprintf(
  48. __('Validation failed for the input string %s.'),
  49. htmlspecialchars($buffer)
  50. );
  51. }
  52. return $buffer;
  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 'Regex Validation';
  63. }
  64. }