weew / config-schema
weew/config 包的 Schema 构建工具。
v1.4.2
2016-08-28 20:26 UTC
Requires
- weew/config: ^1.18
- weew/validator: ^3.7
Requires (Dev)
- henrikbjorn/phpspec-code-coverage: ^2.0
- phpspec/phpspec: ^2.4
- satooshi/php-coveralls: ^0.6.1
- weew/helpers-phpspec: ^1.0
README
目录
安装
composer require weew/config-schema
简介
此包允许轻松进行配置验证,并可与weew/config包一起使用。
使用方法
您可以这样描述您的配置模式
$config = new Config([ 'some' => 'value', 'items' => ['foo', 'bar'], 'name' => 'John Doe', ]); $schema = new ConfigSchema($config); $schema ->hasValue('some') ->hasArray('items')->allowed(['foo', 'baz']) ->hasString('name')->min(3)->max(10) ;
在您描述了模式之后,您可以对其进行验证,这将返回您一个IValidationResult
实例,或者断言它,这将抛出一个异常。
$result = $schema->check(); foreach ($result->getErrors() as $error) { echo $error->getSubject() . ' ' . $error->getMessage(); } // or try { $schema->assert(); } catch (ConfigValidationException $ex) { $result = $ex->getValidationResult(); }