[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
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\Config\Definition; 13 14 use Symfony\Component\Config\Definition\Exception\Exception; 15 use Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException; 16 use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; 17 use Symfony\Component\Config\Definition\Exception\InvalidTypeException; 18 19 /** 20 * The base node class. 21 * 22 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 23 */ 24 abstract class BaseNode implements NodeInterface 25 { 26 protected $name; 27 protected $parent; 28 protected $normalizationClosures = array(); 29 protected $finalValidationClosures = array(); 30 protected $allowOverwrite = true; 31 protected $required = false; 32 protected $equivalentValues = array(); 33 protected $attributes = array(); 34 35 /** 36 * @param string|null $name The name of the node 37 * @param NodeInterface|null $parent The parent of this node 38 * 39 * @throws \InvalidArgumentException if the name contains a period 40 */ 41 public function __construct($name, NodeInterface $parent = null) 42 { 43 if (false !== strpos($name = (string) $name, '.')) { 44 throw new \InvalidArgumentException('The name must not contain ".".'); 45 } 46 47 $this->name = $name; 48 $this->parent = $parent; 49 } 50 51 public function setAttribute($key, $value) 52 { 53 $this->attributes[$key] = $value; 54 } 55 56 public function getAttribute($key, $default = null) 57 { 58 return isset($this->attributes[$key]) ? $this->attributes[$key] : $default; 59 } 60 61 public function hasAttribute($key) 62 { 63 return isset($this->attributes[$key]); 64 } 65 66 public function getAttributes() 67 { 68 return $this->attributes; 69 } 70 71 public function setAttributes(array $attributes) 72 { 73 $this->attributes = $attributes; 74 } 75 76 public function removeAttribute($key) 77 { 78 unset($this->attributes[$key]); 79 } 80 81 /** 82 * Sets an info message. 83 * 84 * @param string $info 85 */ 86 public function setInfo($info) 87 { 88 $this->setAttribute('info', $info); 89 } 90 91 /** 92 * Returns info message. 93 * 94 * @return string The info text 95 */ 96 public function getInfo() 97 { 98 return $this->getAttribute('info'); 99 } 100 101 /** 102 * Sets the example configuration for this node. 103 * 104 * @param string|array $example 105 */ 106 public function setExample($example) 107 { 108 $this->setAttribute('example', $example); 109 } 110 111 /** 112 * Retrieves the example configuration for this node. 113 * 114 * @return string|array The example 115 */ 116 public function getExample() 117 { 118 return $this->getAttribute('example'); 119 } 120 121 /** 122 * Adds an equivalent value. 123 * 124 * @param mixed $originalValue 125 * @param mixed $equivalentValue 126 */ 127 public function addEquivalentValue($originalValue, $equivalentValue) 128 { 129 $this->equivalentValues[] = array($originalValue, $equivalentValue); 130 } 131 132 /** 133 * Set this node as required. 134 * 135 * @param bool $boolean Required node 136 */ 137 public function setRequired($boolean) 138 { 139 $this->required = (bool) $boolean; 140 } 141 142 /** 143 * Sets if this node can be overridden. 144 * 145 * @param bool $allow 146 */ 147 public function setAllowOverwrite($allow) 148 { 149 $this->allowOverwrite = (bool) $allow; 150 } 151 152 /** 153 * Sets the closures used for normalization. 154 * 155 * @param \Closure[] $closures An array of Closures used for normalization 156 */ 157 public function setNormalizationClosures(array $closures) 158 { 159 $this->normalizationClosures = $closures; 160 } 161 162 /** 163 * Sets the closures used for final validation. 164 * 165 * @param \Closure[] $closures An array of Closures used for final validation 166 */ 167 public function setFinalValidationClosures(array $closures) 168 { 169 $this->finalValidationClosures = $closures; 170 } 171 172 /** 173 * {@inheritdoc} 174 */ 175 public function isRequired() 176 { 177 return $this->required; 178 } 179 180 /** 181 * {@inheritdoc} 182 */ 183 public function getName() 184 { 185 return $this->name; 186 } 187 188 /** 189 * {@inheritdoc} 190 */ 191 public function getPath() 192 { 193 $path = $this->name; 194 195 if (null !== $this->parent) { 196 $path = $this->parent->getPath().'.'.$path; 197 } 198 199 return $path; 200 } 201 202 /** 203 * {@inheritdoc} 204 */ 205 final public function merge($leftSide, $rightSide) 206 { 207 if (!$this->allowOverwrite) { 208 throw new ForbiddenOverwriteException(sprintf('Configuration path "%s" cannot be overwritten. You have to define all options for this path, and any of its sub-paths in one configuration section.', $this->getPath())); 209 } 210 211 $this->validateType($leftSide); 212 $this->validateType($rightSide); 213 214 return $this->mergeValues($leftSide, $rightSide); 215 } 216 217 /** 218 * {@inheritdoc} 219 */ 220 final public function normalize($value) 221 { 222 $value = $this->preNormalize($value); 223 224 // run custom normalization closures 225 foreach ($this->normalizationClosures as $closure) { 226 $value = $closure($value); 227 } 228 229 // replace value with their equivalent 230 foreach ($this->equivalentValues as $data) { 231 if ($data[0] === $value) { 232 $value = $data[1]; 233 } 234 } 235 236 // validate type 237 $this->validateType($value); 238 239 // normalize value 240 return $this->normalizeValue($value); 241 } 242 243 /** 244 * Normalizes the value before any other normalization is applied. 245 * 246 * @param $value 247 * 248 * @return The normalized array value 249 */ 250 protected function preNormalize($value) 251 { 252 return $value; 253 } 254 255 /** 256 * Returns parent node for this node. 257 * 258 * @return NodeInterface|null 259 */ 260 public function getParent() 261 { 262 return $this->parent; 263 } 264 265 /** 266 * {@inheritdoc} 267 */ 268 final public function finalize($value) 269 { 270 $this->validateType($value); 271 272 $value = $this->finalizeValue($value); 273 274 // Perform validation on the final value if a closure has been set. 275 // The closure is also allowed to return another value. 276 foreach ($this->finalValidationClosures as $closure) { 277 try { 278 $value = $closure($value); 279 } catch (Exception $e) { 280 throw $e; 281 } catch (\Exception $e) { 282 throw new InvalidConfigurationException(sprintf('Invalid configuration for path "%s": %s', $this->getPath(), $e->getMessage()), $e->getCode(), $e); 283 } 284 } 285 286 return $value; 287 } 288 289 /** 290 * Validates the type of a Node. 291 * 292 * @param mixed $value The value to validate 293 * 294 * @throws InvalidTypeException when the value is invalid 295 */ 296 abstract protected function validateType($value); 297 298 /** 299 * Normalizes the value. 300 * 301 * @param mixed $value The value to normalize 302 * 303 * @return mixed The normalized value 304 */ 305 abstract protected function normalizeValue($value); 306 307 /** 308 * Merges two values together. 309 * 310 * @param mixed $leftSide 311 * @param mixed $rightSide 312 * 313 * @return mixed The merged value 314 */ 315 abstract protected function mergeValues($leftSide, $rightSide); 316 317 /** 318 * Finalizes a value. 319 * 320 * @param mixed $value The value to finalize 321 * 322 * @return mixed The finalized value 323 */ 324 abstract protected function finalizeValue($value); 325 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |