RomaricDrigon\MetaYaml\NodeValidator\ChoiceNodeValidator::validate PHP Метод

validate() публичный Метод

public validate ( $name, $node, $data )
    public function validate($name, $node, $data)
    {
        if ($this->checkRequired($name, $node, $data)) {
            return true;
        }
        $valid = false;
        $message = '';
        $count_levels = -1;
        foreach ($node[$this->schema_validator->getFullName('choices')] as $choice_config) {
            try {
                $this->schema_validator->validateNode($name, $choice_config[$this->schema_validator->getFullName('type')], $choice_config, $data);
                $valid = true;
                break;
            } catch (NodeValidatorException $e) {
                $current_count_levels = count(explode('.', $e->getNodePath()));
                if ($current_count_levels > $count_levels) {
                    $message = $e->getMessage();
                    $count_levels = $current_count_levels;
                }
            }
        }
        if (!$valid) {
            throw new NodeValidatorException($name, "The choice node '{$name}' is invalid with error: {$message}");
        }
        return true;
    }

Usage Example

 public function testAll()
 {
     $this->if($schema_validator = new SchemaValidator())->and($object = new testedClass($schema_validator))->and($config = array('_required' => true, '_choices' => array('a' => array('_type' => 'number'), 'b' => array('_type' => 'boolean'))))->then->boolean($object->validate('toto', $config, 10))->isEqualTo(true)->boolean($object->validate('toto', $config, true))->isEqualTo(true)->exception(function () use($object, $config) {
         $object->validate('test', $config, 'test');
     })->hasMessage("The choice node 'test' is invalid with error: The node 'test' is not a number");
 }
ChoiceNodeValidator