/**
  * Normalizes the value.
  *
  * @param mixed $value The value to normalize
  *
  * @return mixed The normalized value
  *
  * @throws InvalidConfigurationException
  * @throws DuplicateKeyException
  */
 protected function normalizeValue($value)
 {
     if (false === $value) {
         return $value;
     }
     $value = $this->remapXml($value);
     $isAssoc = array_keys($value) !== range(0, count($value) - 1);
     $normalized = array();
     foreach ($value as $k => $v) {
         if (null !== $this->keyAttribute && is_array($v)) {
             if (!isset($v[$this->keyAttribute]) && is_int($k) && !$isAssoc) {
                 $msg = sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath());
                 $ex = new InvalidConfigurationException($msg);
                 $ex->setPath($this->getPath());
                 throw $ex;
             } elseif (isset($v[$this->keyAttribute])) {
                 $k = $v[$this->keyAttribute];
                 // remove the key attribute when required
                 if ($this->removeKeyAttribute) {
                     unset($v[$this->keyAttribute]);
                 }
                 // if only "value" is left
                 if (1 == count($v) && isset($v['value'])) {
                     $v = $v['value'];
                 }
             }
             if (array_key_exists($k, $normalized)) {
                 $msg = sprintf('Duplicate key "%s" for path "%s".', $k, $this->getPath());
                 $ex = new DuplicateKeyException($msg);
                 $ex->setPath($this->getPath());
                 throw $ex;
             }
         }
         $this->prototype->setName($k);
         if (null !== $this->keyAttribute || $isAssoc) {
             $normalized[$k] = $this->prototype->normalize($v);
         } else {
             $normalized[] = $this->prototype->normalize($v);
         }
     }
     return $normalized;
 }
Example #2
0
 /**
  * Normalizes the value.
  *
  * @param mixed $value The value to normalize
  *
  * @return mixed The normalized value
  *
  * @throws InvalidConfigurationException
  * @throws DuplicateKeyException
  */
 protected function normalizeValue($value)
 {
     if (false === $value) {
         return $value;
     }
     $value = $this->remapXml($value);
     $isAssoc = array_keys($value) !== range(0, count($value) - 1);
     $normalized = array();
     foreach ($value as $k => $v) {
         if (null !== $this->keyAttribute && is_array($v)) {
             if (!isset($v[$this->keyAttribute]) && is_int($k) && !$isAssoc) {
                 $msg = sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath());
                 $ex = new InvalidConfigurationException($msg);
                 $ex->setPath($this->getPath());
                 throw $ex;
             } elseif (isset($v[$this->keyAttribute])) {
                 $k = $v[$this->keyAttribute];
                 // remove the key attribute when required
                 if ($this->removeKeyAttribute) {
                     unset($v[$this->keyAttribute]);
                 }
                 // if only "value" is left
                 if (array_keys($v) === array('value')) {
                     $v = $v['value'];
                     if ($this->prototype instanceof ArrayNode && ($children = $this->prototype->getChildren()) && array_key_exists('value', $children)) {
                         $valuePrototype = current($this->valuePrototypes) ?: clone $children['value'];
                         $valuePrototype->parent = $this;
                         $originalClosures = $this->prototype->normalizationClosures;
                         if (is_array($originalClosures)) {
                             $valuePrototypeClosures = $valuePrototype->normalizationClosures;
                             $valuePrototype->normalizationClosures = is_array($valuePrototypeClosures) ? array_merge($originalClosures, $valuePrototypeClosures) : $originalClosures;
                         }
                         $this->valuePrototypes[$k] = $valuePrototype;
                     }
                 }
             }
             if (array_key_exists($k, $normalized)) {
                 $msg = sprintf('Duplicate key "%s" for path "%s".', $k, $this->getPath());
                 $ex = new DuplicateKeyException($msg);
                 $ex->setPath($this->getPath());
                 throw $ex;
             }
         }
         $prototype = $this->getPrototypeForChild($k);
         if (null !== $this->keyAttribute || $isAssoc) {
             $normalized[$k] = $prototype->normalize($v);
         } else {
             $normalized[] = $prototype->normalize($v);
         }
     }
     return $normalized;
 }