1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Finder\Expression;
13:
14: /**
15: * @author Jean-François Simon <contact@jfsimon.fr>
16: */
17: class Expression implements ValueInterface
18: {
19: const TYPE_REGEX = 1;
20: const TYPE_GLOB = 2;
21:
22: /**
23: * @var ValueInterface
24: */
25: private $value;
26:
27: /**
28: * @param string $expr
29: *
30: * @return Expression
31: */
32: public static function create($expr)
33: {
34: return new self($expr);
35: }
36:
37: /**
38: * @param string $expr
39: */
40: public function __construct($expr)
41: {
42: try {
43: $this->value = Regex::create($expr);
44: } catch (\InvalidArgumentException $e) {
45: $this->value = new Glob($expr);
46: }
47: }
48:
49: /**
50: * @return string
51: */
52: public function __toString()
53: {
54: return $this->render();
55: }
56:
57: /**
58: * {@inheritdoc}
59: */
60: public function render()
61: {
62: return $this->value->render();
63: }
64:
65: /**
66: * {@inheritdoc}
67: */
68: public function renderPattern()
69: {
70: return $this->value->renderPattern();
71: }
72:
73: /**
74: * @return bool
75: */
76: public function isCaseSensitive()
77: {
78: return $this->value->isCaseSensitive();
79: }
80:
81: /**
82: * @return int
83: */
84: public function getType()
85: {
86: return $this->value->getType();
87: }
88:
89: /**
90: * {@inheritdoc}
91: */
92: public function prepend($expr)
93: {
94: $this->value->prepend($expr);
95:
96: return $this;
97: }
98:
99: /**
100: * {@inheritdoc}
101: */
102: public function append($expr)
103: {
104: $this->value->append($expr);
105:
106: return $this;
107: }
108:
109: /**
110: * @return bool
111: */
112: public function isRegex()
113: {
114: return self::TYPE_REGEX === $this->value->getType();
115: }
116:
117: /**
118: * @return bool
119: */
120: public function isGlob()
121: {
122: return self::TYPE_GLOB === $this->value->getType();
123: }
124:
125: /**
126: * @throws \LogicException
127: *
128: * @return Glob
129: */
130: public function getGlob()
131: {
132: if (self::TYPE_GLOB !== $this->value->getType()) {
133: throw new \LogicException('Regex cant be transformed to glob.');
134: }
135:
136: return $this->value;
137: }
138:
139: /**
140: * @return Regex
141: */
142: public function getRegex()
143: {
144: return self::TYPE_REGEX === $this->value->getType() ? $this->value : $this->value->toRegex();
145: }
146: }
147: