RegexValidationTransformationsPlugin.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Abstract class for the regex validation input transformations plugins
  5. *
  6. * @package PhpMyAdmin-Transformations
  7. * @subpackage RegexValidation
  8. */
  9. declare(strict_types=1);
  10. namespace PhpMyAdmin\Plugins\Transformations\Abs;
  11. use PhpMyAdmin\Plugins\IOTransformationsPlugin;
  12. use stdClass;
  13. /**
  14. * Provides common methods for all of the regex validation
  15. * input transformations plugins.
  16. *
  17. * @package PhpMyAdmin-Transformations
  18. * @subpackage RegexValidation
  19. */
  20. abstract class RegexValidationTransformationsPlugin extends IOTransformationsPlugin
  21. {
  22. /**
  23. * Gets the transformation description of the specific plugin
  24. *
  25. * @return string
  26. */
  27. public static function getInfo()
  28. {
  29. return __(
  30. 'Validates the string using regular expression '
  31. . 'and performs insert only if string matches it. '
  32. . 'The first option is the Regular Expression.'
  33. );
  34. }
  35. /**
  36. * Does the actual work of each specific transformations plugin.
  37. *
  38. * @param string $buffer text to be transformed
  39. * @param array $options transformation options
  40. * @param stdClass|null $meta meta information
  41. *
  42. * @return string
  43. */
  44. public function applyTransformation($buffer, array $options = [], ?stdClass $meta = null)
  45. {
  46. // reset properties of object
  47. $this->reset();
  48. if (! empty($options[0]) && ! preg_match($options[0], $buffer)) {
  49. $this->success = false;
  50. $this->error = sprintf(
  51. __('Validation failed for the input string %s.'),
  52. htmlspecialchars($buffer)
  53. );
  54. }
  55. return $buffer;
  56. }
  57. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  58. /**
  59. * Gets the transformation name of the specific plugin
  60. *
  61. * @return string
  62. */
  63. public static function getName()
  64. {
  65. return "Regex Validation";
  66. }
  67. }