4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Expression;
14 @trigger_error('The '.__NAMESPACE__.'\Regex class is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
17 * @author Jean-François Simon <contact@jfsimon.fr>
19 class Regex implements ValueInterface
21 const START_FLAG = '^';
25 const ESCAPING = '\\';
62 * @throws \InvalidArgumentException
64 public static function create($expr)
66 if (preg_match('/^(.{3,}?)([imsxuADU]*)$/', $expr, $m)) {
67 $start = substr($m[1], 0, 1);
68 $end = substr($m[1], -1);
71 ($start === $end && !preg_match('/[*?[:alnum:] \\\\]/', $start))
72 || ($start === '{' && $end === '}')
73 || ($start === '(' && $end === ')')
75 return new self(substr($m[1], 1, -1), $m[2], $end);
79 throw new \InvalidArgumentException('Given expression is not a regex.');
83 * @param string $pattern
84 * @param string $options
85 * @param string $delimiter
87 public function __construct($pattern, $options = '', $delimiter = null)
89 if (null !== $delimiter) {
90 // removes delimiter escaping
91 $pattern = str_replace('\\'.$delimiter, $delimiter, $pattern);
94 $this->parsePattern($pattern);
95 $this->options = $options;
101 public function __toString()
103 return $this->render();
109 public function render()
111 return self::BOUNDARY
112 .$this->renderPattern()
120 public function renderPattern()
122 return ($this->startFlag ? self::START_FLAG : '')
123 .($this->startJoker ? self::JOKER : '')
124 .str_replace(self::BOUNDARY, '\\'.self::BOUNDARY, $this->pattern)
125 .($this->endJoker ? self::JOKER : '')
126 .($this->endFlag ? self::END_FLAG : '');
132 public function isCaseSensitive()
134 return !$this->hasOption('i');
140 public function getType()
142 return Expression::TYPE_REGEX;
148 public function prepend($expr)
150 $this->pattern = $expr.$this->pattern;
158 public function append($expr)
160 $this->pattern .= $expr;
166 * @param string $option
170 public function hasOption($option)
172 return false !== strpos($this->options, $option);
176 * @param string $option
180 public function addOption($option)
182 if (!$this->hasOption($option)) {
183 $this->options .= $option;
190 * @param string $option
194 public function removeOption($option)
196 $this->options = str_replace($option, '', $this->options);
202 * @param bool $startFlag
206 public function setStartFlag($startFlag)
208 $this->startFlag = $startFlag;
216 public function hasStartFlag()
218 return $this->startFlag;
222 * @param bool $endFlag
226 public function setEndFlag($endFlag)
228 $this->endFlag = (bool) $endFlag;
236 public function hasEndFlag()
238 return $this->endFlag;
242 * @param bool $startJoker
246 public function setStartJoker($startJoker)
248 $this->startJoker = $startJoker;
256 public function hasStartJoker()
258 return $this->startJoker;
262 * @param bool $endJoker
266 public function setEndJoker($endJoker)
268 $this->endJoker = (bool) $endJoker;
276 public function hasEndJoker()
278 return $this->endJoker;
282 * @param array $replacement
286 public function replaceJokers($replacement)
288 $replace = function ($subject) use ($replacement) {
289 $subject = $subject[0];
290 $replace = 0 === substr_count($subject, '\\') % 2;
292 return $replace ? str_replace('.', $replacement, $subject) : $subject;
295 $this->pattern = preg_replace_callback('~[\\\\]*\\.~', $replace, $this->pattern);
301 * @param string $pattern
303 private function parsePattern($pattern)
305 if ($this->startFlag = self::START_FLAG === substr($pattern, 0, 1)) {
306 $pattern = substr($pattern, 1);
309 if ($this->startJoker = self::JOKER === substr($pattern, 0, 2)) {
310 $pattern = substr($pattern, 2);
313 if ($this->endFlag = (self::END_FLAG === substr($pattern, -1) && self::ESCAPING !== substr($pattern, -2, -1))) {
314 $pattern = substr($pattern, 0, -1);
317 if ($this->endJoker = (self::JOKER === substr($pattern, -2) && self::ESCAPING !== substr($pattern, -3, -2))) {
318 $pattern = substr($pattern, 0, -2);
321 $this->pattern = $pattern;