Esempio n. 1
0
 public function testNormalizeThrowsExceptionWithErrorMessage()
 {
     $node = new ScalarNode('test');
     $node->setInfo('"the test value"');
     $this->setExpectedException('Symfony\\Component\\Config\\Definition\\Exception\\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
     $node->normalize(array());
 }
Esempio n. 2
0
 /**
  * {@inheritDoc}
  */
 protected function validateType($value)
 {
     parent::validateType($value);
     if (!is_bool($value)) {
         throw new InvalidTypeException(sprintf('Invalid type for path "%s". Expected boolean, but got %s.', $this->getPath(), json_encode($value)));
     }
 }
Esempio n. 3
0
 protected function finalizeValue($value)
 {
     $value = parent::finalizeValue($value);
     if (!in_array($value, $this->values, true)) {
         $ex = new InvalidConfigurationException(sprintf('The value %s is not allowed for path "%s". Permissible values: %s', json_encode($value), $this->getPath(), implode(', ', array_map('json_encode', $this->values))));
         $ex->setPath($this->getPath());
         throw $ex;
     }
     return $value;
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 protected function finalizeValue($value)
 {
     $value = parent::finalizeValue($value);
     $errorMsg = null;
     if (isset($this->min) && $value < $this->min) {
         $errorMsg = sprintf('The value %s is too small for path "%s". Should be greater than or equal to %s', $value, $this->getPath(), $this->min);
     }
     if (isset($this->max) && $value > $this->max) {
         $errorMsg = sprintf('The value %s is too big for path "%s". Should be less than or equal to %s', $value, $this->getPath(), $this->max);
     }
     if (isset($errorMsg)) {
         $ex = new InvalidConfigurationException($errorMsg);
         $ex->setPath($this->getPath());
         throw $ex;
     }
     return $value;
 }
Esempio n. 5
0
 /**
  * @param PrototypedArrayNode $node
  *
  * @return array
  */
 private function getPrototypeChildren(PrototypedArrayNode $node)
 {
     $prototype = $node->getPrototype();
     $key = $node->getKeyAttribute();
     // Do not expand prototype if it isn't an array node nor uses attribute as key
     if (!$key && !$prototype instanceof ArrayNode) {
         return $node->getChildren();
     }
     if ($prototype instanceof ArrayNode) {
         $keyNode = new ArrayNode($key, $node);
         $children = $prototype->getChildren();
         if ($prototype instanceof PrototypedArrayNode && $prototype->getKeyAttribute()) {
             $children = $this->getPrototypeChildren($prototype);
         }
         // add children
         foreach ($children as $childNode) {
             $keyNode->addChild($childNode);
         }
     } else {
         $keyNode = new ScalarNode($key, $node);
     }
     $info = 'Prototype';
     if (null !== $prototype->getInfo()) {
         $info .= ': ' . $prototype->getInfo();
     }
     $keyNode->setInfo($info);
     return array($key => $keyNode);
 }
Esempio n. 6
0
 /**
  * @dataProvider getEmptyValues
  * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  *
  * @param mixed $value
  */
 public function testNotAllowedEmptyValuesThrowException($value)
 {
     $node = new ScalarNode('test');
     $node->setAllowEmptyValue(false);
     $node->finalize($value);
 }
Esempio n. 7
0
 /**
  * @dataProvider getInvalidValues
  * @expectedException Symfony\Component\Config\Definition\Exception\InvalidTypeException
  */
 public function testNormalizeThrowsExceptionOnInvalidValues($value)
 {
     $node = new ScalarNode('test');
     $node->normalize($value);
 }
 protected function getPrototypeNodeWithDefaultChildren()
 {
     $node = new PrototypedArrayNode('root');
     $prototype = new ArrayNode(null, $node);
     $child = new ScalarNode('foo');
     $child->setDefaultValue('bar');
     $prototype->addChild($child);
     $prototype->setAddIfNotSet(true);
     $node->setPrototype($prototype);
     return $node;
 }
Esempio n. 9
0
 /**
  * Configures a scalar node.
  *
  * @param ScalarNode  $configNode The node to configure
  * @param NodeBuilder $node       The builder of the node
  */
 protected function configureScalarNode(ScalarNode $configNode, NodeBuilder $node)
 {
     if (null !== $node->normalization) {
         $configNode->setNormalizationClosures($this->buildExpressions($node->normalization->before));
     }
     if (null !== $node->merge) {
         $configNode->setAllowOverwrite($node->merge->allowOverwrite);
     }
     if (true === $node->default) {
         $configNode->setDefaultValue($node->defaultValue);
     }
     if (false === $node->allowEmptyValue) {
         $configNode->setAllowEmptyValue($node->allowEmptyValue);
     }
     $configNode->addEquivalentValue(null, $node->nullEquivalent);
     $configNode->addEquivalentValue(true, $node->trueEquivalent);
     $configNode->addEquivalentValue(false, $node->falseEquivalent);
     $configNode->setRequired($node->required);
     if (null !== $node->validation) {
         $configNode->setFinalValidationClosures($this->buildExpressions($node->validation->rules));
     }
 }