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