Debian Codegraph @

lib/3rdParty/composer/symfony/property-access/PropertyAccessorBuilder.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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\PropertyAccess;
13
14 use Psr\Cache\CacheItemPoolInterface;
15 use Symfony\Component\PropertyInfo\PropertyReadInfoExtractorInterface;
16 use Symfony\Component\PropertyInfo\PropertyWriteInfoExtractorInterface;
17
18 /**
19 * A configurable builder to create a PropertyAccessor.
20 *
21 * @author Jérémie Augustin <[email protected]m>
22 */
23 class PropertyAccessorBuilder
24 {
25 private int $magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET;
26 private bool $throwExceptionOnInvalidIndex = false;
27 private bool $throwExceptionOnInvalidPropertyPath = true;
28 private ?CacheItemPoolInterface $cacheItemPool = null;
29 private ?PropertyReadInfoExtractorInterface $readInfoExtractor = null;
30 private ?PropertyWriteInfoExtractorInterface $writeInfoExtractor = null;
31
32 /**
33 * Enables the use of all magic methods by the PropertyAccessor.
34 *
35 * @return $this
36 */
37 public function enableMagicMethods(): static
38 {
39 $this->magicMethods = PropertyAccessor::MAGIC_GET | PropertyAccessor::MAGIC_SET | PropertyAccessor::MAGIC_CALL;
40
41 return $this;
42 }
43
44 /**
45 * Disable the use of all magic methods by the PropertyAccessor.
46 *
47 * @return $this
48 */
49 public function disableMagicMethods(): static
50 {
51 $this->magicMethods = PropertyAccessor::DISALLOW_MAGIC_METHODS;
52
53 return $this;
54 }
55
56 /**
57 * Enables the use of "__call" by the PropertyAccessor.
58 *
59 * @return $this
60 */
61 public function enableMagicCall(): static
62 {
63 $this->magicMethods |= PropertyAccessor::MAGIC_CALL;
64
65 return $this;
66 }
67
68 /**
69 * Enables the use of "__get" by the PropertyAccessor.
70 */
71 public function enableMagicGet(): self
72 {
73 $this->magicMethods |= PropertyAccessor::MAGIC_GET;
74
75 return $this;
76 }
77
78 /**
79 * Enables the use of "__set" by the PropertyAccessor.
80 *
81 * @return $this
82 */
83 public function enableMagicSet(): static
84 {
85 $this->magicMethods |= PropertyAccessor::MAGIC_SET;
86
87 return $this;
88 }
89
90 /**
91 * Disables the use of "__call" by the PropertyAccessor.
92 *
93 * @return $this
94 */
95 public function disableMagicCall(): static
96 {
97 $this->magicMethods &= ~PropertyAccessor::MAGIC_CALL;
98
99 return $this;
100 }
101
102 /**
103 * Disables the use of "__get" by the PropertyAccessor.
104 *
105 * @return $this
106 */
107 public function disableMagicGet(): static
108 {
109 $this->magicMethods &= ~PropertyAccessor::MAGIC_GET;
110
111 return $this;
112 }
113
114 /**
115 * Disables the use of "__set" by the PropertyAccessor.
116 *
117 * @return $this
118 */
119 public function disableMagicSet(): static
120 {
121 $this->magicMethods &= ~PropertyAccessor::MAGIC_SET;
122
123 return $this;
124 }
125
126 /**
127 * @return bool whether the use of "__call" by the PropertyAccessor is enabled
128 */
129 public function isMagicCallEnabled(): bool
130 {
131 return $this->magicMethods & PropertyAccessor::MAGIC_CALL;
132 }
133
134 /**
135 * @return bool whether the use of "__get" by the PropertyAccessor is enabled
136 */
137 public function isMagicGetEnabled(): bool
138 {
139 return $this->magicMethods & PropertyAccessor::MAGIC_GET;
140 }
141
142 /**
143 * @return bool whether the use of "__set" by the PropertyAccessor is enabled
144 */
145 public function isMagicSetEnabled(): bool
146 {
147 return $this->magicMethods & PropertyAccessor::MAGIC_SET;
148 }
149
150 /**
151 * Enables exceptions when reading a non-existing index.
152 *
153 * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
154 * which are always created on-the-fly.
155 *
156 * @return $this
157 */
158 public function enableExceptionOnInvalidIndex(): static
159 {
160 $this->throwExceptionOnInvalidIndex = true;
161
162 return $this;
163 }
164
165 /**
166 * Disables exceptions when reading a non-existing index.
167 *
168 * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
169 *
170 * @return $this
171 */
172 public function disableExceptionOnInvalidIndex(): static
173 {
174 $this->throwExceptionOnInvalidIndex = false;
175
176 return $this;
177 }
178
179 /**
180 * @return bool whether an exception is thrown or null is returned when reading a non-existing index
181 */
182 public function isExceptionOnInvalidIndexEnabled(): bool
183 {
184 return $this->throwExceptionOnInvalidIndex;
185 }
186
187 /**
188 * Enables exceptions when reading a non-existing property.
189 *
190 * This has no influence on writing non-existing indices with PropertyAccessorInterface::setValue()
191 * which are always created on-the-fly.
192 *
193 * @return $this
194 */
195 public function enableExceptionOnInvalidPropertyPath(): static
196 {
197 $this->throwExceptionOnInvalidPropertyPath = true;
198
199 return $this;
200 }
201
202 /**
203 * Disables exceptions when reading a non-existing index.
204 *
205 * Instead, null is returned when calling PropertyAccessorInterface::getValue() on a non-existing index.
206 *
207 * @return $this
208 */
209 public function disableExceptionOnInvalidPropertyPath(): static
210 {
211 $this->throwExceptionOnInvalidPropertyPath = false;
212
213 return $this;
214 }
215
216 /**
217 * @return bool whether an exception is thrown or null is returned when reading a non-existing property
218 */
219 public function isExceptionOnInvalidPropertyPath(): bool
220 {
221 return $this->throwExceptionOnInvalidPropertyPath;
222 }
223
224 /**
225 * Sets a cache system.
226 *
227 * @return $this
228 */
229 public function setCacheItemPool(?CacheItemPoolInterface $cacheItemPool): static
230 {
231 $this->cacheItemPool = $cacheItemPool;
232
233 return $this;
234 }
235
236 /**
237 * Gets the used cache system.
238 */
239 public function getCacheItemPool(): ?CacheItemPoolInterface
240 {
241 return $this->cacheItemPool;
242 }
243
244 /**
245 * @return $this
246 */
247 public function setReadInfoExtractor(?PropertyReadInfoExtractorInterface $readInfoExtractor): static
248 {
249 $this->readInfoExtractor = $readInfoExtractor;
250
251 return $this;
252 }
253
254 public function getReadInfoExtractor(): ?PropertyReadInfoExtractorInterface
255 {
256 return $this->readInfoExtractor;
257 }
258
259 /**
260 * @return $this
261 */
262 public function setWriteInfoExtractor(?PropertyWriteInfoExtractorInterface $writeInfoExtractor): static
263 {
264 $this->writeInfoExtractor = $writeInfoExtractor;
265
266 return $this;
267 }
268
269 public function getWriteInfoExtractor(): ?PropertyWriteInfoExtractorInterface
270 {
271 return $this->writeInfoExtractor;
272 }
273
274 /**
275 * Builds and returns a new PropertyAccessor object.
276 */
277 public function getPropertyAccessor(): PropertyAccessorInterface
278 {
279 $throw = PropertyAccessor::DO_NOT_THROW;
280
281 if ($this->throwExceptionOnInvalidIndex) {
282 $throw |= PropertyAccessor::THROW_ON_INVALID_INDEX;
283 }
284
285 if ($this->throwExceptionOnInvalidPropertyPath) {
286 $throw |= PropertyAccessor::THROW_ON_INVALID_PROPERTY_PATH;
287 }
288
289 return new PropertyAccessor($this->magicMethods, $throw, $this->cacheItemPool, $this->readInfoExtractor, $this->writeInfoExtractor);
290 }
291 }