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