RomaricDrigon\MetaYaml\NodeValidator\ArrayNodeValidator::validate PHP Method

validate() public method

public validate ( $name, $node, $data )
    public function validate($name, $node, $data)
    {
        if ($this->checkRequired($name, $node, $data)) {
            return true;
        }
        if (!is_array($data)) {
            throw new NodeValidatorException($name, "The node '{$name}' is not an array");
        }
        $this->checkEmpty($name, $node, $data);
        foreach ($node[$this->schema_validator->getFullName('children')] as $key => $value) {
            $this->schema_validator->validateNode($name . '.' . $key, $value[$this->schema_validator->getFullName('type')], $value, isset($data[$key]) ? $data[$key] : null);
            if (array_key_exists($key, $data)) {
                unset($data[$key]);
            }
        }
        if (count($data) === 0 || isset($node[$this->schema_validator->getFullName('ignore_extra_keys')]) && $node[$this->schema_validator->getFullName('ignore_extra_keys')]) {
            return true;
            // we skip the next check
        }
        throw new NodeValidatorException($name, "The node '{$name}' has not allowed extra key(s): " . implode(', ', array_keys($data)));
    }

Usage Example

 public function testIgnoreExtraKeys()
 {
     $this->if($schema_validator = new SchemaValidator())->and($object = new testedClass($schema_validator))->and($config = array('_ignore_extra_keys' => true, '_children' => array('a' => array('_type' => 'text'))))->then->boolean($object->validate('test', $config, array('a' => 'test', 'b' => 'test2')))->isEqualTo(true);
 }
ArrayNodeValidator