Drupal investigation

Regex.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Constraints;
  11. use Symfony\Component\Validator\Constraint;
  12. /**
  13. * @Annotation
  14. * @Target({"PROPERTY", "METHOD", "ANNOTATION"})
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class Regex extends Constraint
  19. {
  20. const REGEX_FAILED_ERROR = 'de1e3db3-5ed4-4941-aae4-59f3667cc3a3';
  21. protected static $errorNames = array(
  22. self::REGEX_FAILED_ERROR => 'REGEX_FAILED_ERROR',
  23. );
  24. public $message = 'This value is not valid.';
  25. public $pattern;
  26. public $htmlPattern;
  27. public $match = true;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getDefaultOption()
  32. {
  33. return 'pattern';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getRequiredOptions()
  39. {
  40. return array('pattern');
  41. }
  42. /**
  43. * Converts the htmlPattern to a suitable format for HTML5 pattern.
  44. * Example: /^[a-z]+$/ would be converted to [a-z]+
  45. * However, if options are specified, it cannot be converted.
  46. *
  47. * Pattern is also ignored if match=false since the pattern should
  48. * then be reversed before application.
  49. *
  50. * @see http://dev.w3.org/html5/spec/single-page.html#the-pattern-attribute
  51. *
  52. * @return string|null
  53. */
  54. public function getHtmlPattern()
  55. {
  56. // If htmlPattern is specified, use it
  57. if (null !== $this->htmlPattern) {
  58. return empty($this->htmlPattern)
  59. ? null
  60. : $this->htmlPattern;
  61. }
  62. // Quit if delimiters not at very beginning/end (e.g. when options are passed)
  63. if ($this->pattern[0] !== $this->pattern[strlen($this->pattern) - 1]) {
  64. return;
  65. }
  66. $delimiter = $this->pattern[0];
  67. // Unescape the delimiter
  68. $pattern = str_replace('\\'.$delimiter, $delimiter, substr($this->pattern, 1, -1));
  69. // If the pattern is inverted, we can simply wrap it in
  70. // ((?!pattern).)*
  71. if (!$this->match) {
  72. return '((?!'.$pattern.').)*';
  73. }
  74. // If the pattern contains an or statement, wrap the pattern in
  75. // .*(pattern).* and quit. Otherwise we'd need to parse the pattern
  76. if (false !== strpos($pattern, '|')) {
  77. return '.*('.$pattern.').*';
  78. }
  79. // Trim leading ^, otherwise prepend .*
  80. $pattern = '^' === $pattern[0] ? substr($pattern, 1) : '.*'.$pattern;
  81. // Trim trailing $, otherwise append .*
  82. $pattern = '$' === $pattern[strlen($pattern) - 1] ? substr($pattern, 0, -1) : $pattern.'.*';
  83. return $pattern;
  84. }
  85. }