Example #1
0
 /**
  * Remaps multiple singular values to a single plural value.
  *
  * @param array $value The source values
  *
  * @return array The remapped values
  */
 protected function remapXml($value)
 {
     foreach ($this->xmlRemappings as $transformation) {
         list($singular, $plural) = $transformation;
         if (!isset($value[$singular])) {
             continue;
         }
         $value[$plural] = Processor::normalizeConfig($value, $singular, $plural);
         unset($value[$singular]);
     }
     return $value;
 }
Example #2
0
    /**
     * Normalizes the value.
     *
     * @param mixed $value The value to normalize
     * @return mixed The normalized value
     */
    protected function normalizeValue($value)
    {
        if (false === $value) {
            return $value;
        }

        foreach ($this->xmlRemappings as $transformation) {
            list($singular, $plural) = $transformation;

            if (!isset($value[$singular])) {
                continue;
            }

            $value[$plural] = Processor::normalizeConfig($value, $singular, $plural);
            unset($value[$singular]);
        }

        $normalized = array();
        if (null !== $this->prototype) {            
            foreach ($value as $k => $v) {
                if (null !== $this->keyAttribute && is_array($v)) {
                    if (!isset($v[$this->keyAttribute]) && is_int($k)) {
                        $msg = sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath());
                        throw new InvalidConfigurationException($msg);
                    } else if (isset($v[$this->keyAttribute])) {
                        $k = $v[$this->keyAttribute];

                        // remove the key attribute if configured to
                        if ($this->removeKeyAttribute) {
                            unset($v[$this->keyAttribute]);
                        }
                    }

                    if (array_key_exists($k, $normalized)) {
                        $msg = sprintf('Duplicate key "%s" for path "%s".', $k, $this->getPath());
                        throw new DuplicateKeyException($msg);
                    }
                }

                $this->prototype->setName($k);
                if (null !== $this->keyAttribute) {
                    $normalized[$k] = $this->prototype->normalize($v);
                } else {
                    $normalized[] = $this->prototype->normalize($v);
                }
            }
        } else {
            foreach ($this->children as $name => $child) {
                if (array_key_exists($name, $value)) {
                    $normalized[$name] = $child->normalize($value[$name]);
                    unset($value[$name]);
                }
            }

            // if extra fields are present, throw exception
            if (count($value) && !$this->ignoreExtraKeys) {
                $msg = sprintf('Unrecognized options "%s" under "%s"', implode(', ', array_keys($value)), $this->getPath());
                throw new InvalidConfigurationException($msg);
            }
        }

        return $normalized;
    }