Drupal investigation

Regex.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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\Finder\Expression;
  11. @trigger_error('The '.__NAMESPACE__.'\Regex class is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
  12. /**
  13. * @author Jean-François Simon <contact@jfsimon.fr>
  14. */
  15. class Regex implements ValueInterface
  16. {
  17. const START_FLAG = '^';
  18. const END_FLAG = '$';
  19. const BOUNDARY = '~';
  20. const JOKER = '.*';
  21. const ESCAPING = '\\';
  22. /**
  23. * @var string
  24. */
  25. private $pattern;
  26. /**
  27. * @var array
  28. */
  29. private $options;
  30. /**
  31. * @var bool
  32. */
  33. private $startFlag;
  34. /**
  35. * @var bool
  36. */
  37. private $endFlag;
  38. /**
  39. * @var bool
  40. */
  41. private $startJoker;
  42. /**
  43. * @var bool
  44. */
  45. private $endJoker;
  46. /**
  47. * @param string $expr
  48. *
  49. * @return self
  50. *
  51. * @throws \InvalidArgumentException
  52. */
  53. public static function create($expr)
  54. {
  55. if (preg_match('/^(.{3,}?)([imsxuADU]*)$/', $expr, $m)) {
  56. $start = substr($m[1], 0, 1);
  57. $end = substr($m[1], -1);
  58. if (
  59. ($start === $end && !preg_match('/[*?[:alnum:] \\\\]/', $start))
  60. || ($start === '{' && $end === '}')
  61. || ($start === '(' && $end === ')')
  62. ) {
  63. return new self(substr($m[1], 1, -1), $m[2], $end);
  64. }
  65. }
  66. throw new \InvalidArgumentException('Given expression is not a regex.');
  67. }
  68. /**
  69. * @param string $pattern
  70. * @param string $options
  71. * @param string $delimiter
  72. */
  73. public function __construct($pattern, $options = '', $delimiter = null)
  74. {
  75. if (null !== $delimiter) {
  76. // removes delimiter escaping
  77. $pattern = str_replace('\\'.$delimiter, $delimiter, $pattern);
  78. }
  79. $this->parsePattern($pattern);
  80. $this->options = $options;
  81. }
  82. /**
  83. * @return string
  84. */
  85. public function __toString()
  86. {
  87. return $this->render();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function render()
  93. {
  94. return self::BOUNDARY
  95. .$this->renderPattern()
  96. .self::BOUNDARY
  97. .$this->options;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function renderPattern()
  103. {
  104. return ($this->startFlag ? self::START_FLAG : '')
  105. .($this->startJoker ? self::JOKER : '')
  106. .str_replace(self::BOUNDARY, '\\'.self::BOUNDARY, $this->pattern)
  107. .($this->endJoker ? self::JOKER : '')
  108. .($this->endFlag ? self::END_FLAG : '');
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function isCaseSensitive()
  114. {
  115. return !$this->hasOption('i');
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function getType()
  121. {
  122. return Expression::TYPE_REGEX;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function prepend($expr)
  128. {
  129. $this->pattern = $expr.$this->pattern;
  130. return $this;
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function append($expr)
  136. {
  137. $this->pattern .= $expr;
  138. return $this;
  139. }
  140. /**
  141. * @param string $option
  142. *
  143. * @return bool
  144. */
  145. public function hasOption($option)
  146. {
  147. return false !== strpos($this->options, $option);
  148. }
  149. /**
  150. * @param string $option
  151. *
  152. * @return $this
  153. */
  154. public function addOption($option)
  155. {
  156. if (!$this->hasOption($option)) {
  157. $this->options .= $option;
  158. }
  159. return $this;
  160. }
  161. /**
  162. * @param string $option
  163. *
  164. * @return $this
  165. */
  166. public function removeOption($option)
  167. {
  168. $this->options = str_replace($option, '', $this->options);
  169. return $this;
  170. }
  171. /**
  172. * @param bool $startFlag
  173. *
  174. * @return $this
  175. */
  176. public function setStartFlag($startFlag)
  177. {
  178. $this->startFlag = $startFlag;
  179. return $this;
  180. }
  181. /**
  182. * @return bool
  183. */
  184. public function hasStartFlag()
  185. {
  186. return $this->startFlag;
  187. }
  188. /**
  189. * @param bool $endFlag
  190. *
  191. * @return $this
  192. */
  193. public function setEndFlag($endFlag)
  194. {
  195. $this->endFlag = (bool) $endFlag;
  196. return $this;
  197. }
  198. /**
  199. * @return bool
  200. */
  201. public function hasEndFlag()
  202. {
  203. return $this->endFlag;
  204. }
  205. /**
  206. * @param bool $startJoker
  207. *
  208. * @return $this
  209. */
  210. public function setStartJoker($startJoker)
  211. {
  212. $this->startJoker = $startJoker;
  213. return $this;
  214. }
  215. /**
  216. * @return bool
  217. */
  218. public function hasStartJoker()
  219. {
  220. return $this->startJoker;
  221. }
  222. /**
  223. * @param bool $endJoker
  224. *
  225. * @return $this
  226. */
  227. public function setEndJoker($endJoker)
  228. {
  229. $this->endJoker = (bool) $endJoker;
  230. return $this;
  231. }
  232. /**
  233. * @return bool
  234. */
  235. public function hasEndJoker()
  236. {
  237. return $this->endJoker;
  238. }
  239. /**
  240. * @param array $replacement
  241. *
  242. * @return $this
  243. */
  244. public function replaceJokers($replacement)
  245. {
  246. $replace = function ($subject) use ($replacement) {
  247. $subject = $subject[0];
  248. $replace = 0 === substr_count($subject, '\\') % 2;
  249. return $replace ? str_replace('.', $replacement, $subject) : $subject;
  250. };
  251. $this->pattern = preg_replace_callback('~[\\\\]*\\.~', $replace, $this->pattern);
  252. return $this;
  253. }
  254. /**
  255. * @param string $pattern
  256. */
  257. private function parsePattern($pattern)
  258. {
  259. if ($this->startFlag = self::START_FLAG === substr($pattern, 0, 1)) {
  260. $pattern = substr($pattern, 1);
  261. }
  262. if ($this->startJoker = self::JOKER === substr($pattern, 0, 2)) {
  263. $pattern = substr($pattern, 2);
  264. }
  265. if ($this->endFlag = (self::END_FLAG === substr($pattern, -1) && self::ESCAPING !== substr($pattern, -2, -1))) {
  266. $pattern = substr($pattern, 0, -1);
  267. }
  268. if ($this->endJoker = (self::JOKER === substr($pattern, -2) && self::ESCAPING !== substr($pattern, -3, -2))) {
  269. $pattern = substr($pattern, 0, -2);
  270. }
  271. $this->pattern = $pattern;
  272. }
  273. }