Regex.php 6.2 KB

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