麻烦使用lezhnev74 / pasvl进行PHP数组验证

时间:2018-01-27 17:54:07

标签: php arrays validation

希望我不会在这里做些傻事,但我无法按预期工作https://github.com/lezhnev74/pasvl(错误如下)。我想利用这个github项目提供的高级数组验证。

这就是我正在做的事情:

mkdir test
cd test
composer require lezhnev74/pasvl

PHP代码(来自github自述文件的示例代码)如下:

<?php
require_once("vendor/autoload.php");

use \PASVL\Traverser\TraversingMatcher;
use \PASVL\ValidatorLocator\ValidatorLocator;

$data = [
    [
        'type' => 'book',
        'title' => 'Geography book',
        'chapters' => [
            'eu' => ['title' => 'Europe', 'interesting' => true],
            'as' => ['title' => 'America', 'interesting' => false],
        ],
    ],
    [
        'type' => 'book',
        'title' => 'Foreign languages book',
        'chapters' => [
            'de' => ['title' => 'Deutsch'],
        ],
    ],
];

$pattern = [
    '*' => [
        'type' => 'book',
        'title' => ':string :contains(book)',
        'chapters' => [
            ':string :length(2) {1,3}' => [
                'title' => ':string',
                'interesting?' => ':bool',
            ],
        ],
    ],
];

$traverser = new TraversingMatcher(new ValidatorLocator());
$traverser->match($pattern, $data);

......和错误:

% php a.php

Fatal error: Uncaught TypeError: Return value of PASVL\ValidatorLocator\ValidatorLocator::locateDefault() must be an instance of PASVL\ValidatorLocator\object or null, instance of PASVL\Validator\AnyValidator returned in test/vendor/lezhnev74/pasvl/src/ValidatorLocator/ValidatorLocator.php:36
Stack trace:
#0 test/vendor/lezhnev74/pasvl/src/ValidatorLocator/ValidatorLocator.php(22): PASVL\ValidatorLocator\ValidatorLocator->locateDefault('any')
#1 test/vendor/lezhnev74/pasvl/src/Traverser/TraversingMatcher.php(216): PASVL\ValidatorLocator\ValidatorLocator->getValidatorClass('any')
#2 test/vendor/lezhnev74/pasvl/src/Traverser/TraversingMatcher.php(128): PASVL\Traverser\TraversingMatcher->findMatchedPatterns(0, Array)
#3 test/vendor/lezhnev74/pasvl/src/Traverser/TraversingMatcher.php(44): PASVL\Traverser\TraversingMatcher->matchDataToPattern(Array, Array)
#4 test/a.php(39): PASVL\Traverser\TraversingMatcher->match(Array, Array)
#5 {main}
  thrown in test/vendor/lezhnev74/pasvl/src/ValidatorLocator/ValidatorLocator.php on line 36

PHP版本:

% php --version
PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

我很感激任何指示。

由于

1 个答案:

答案 0 :(得分:0)

作者提供了修复,感谢Dmitriy!

更新composer.json:

"require": {
   "lezhnev74/pasvl": "~0.3.0"
}