配置结构的处理器系统

dev-master / 1.0.x-dev 2018-05-20 18:52 UTC

This package is not auto-updated.

Last update: 2024-09-15 04:52:35 UTC


README

配置结构的处理器

关键概念

配置的关键概念是在另一个元素内部添加元素,遵循复合结构模式。

每个配置元素将指定一个允许的类型列表。

$config = new Configuration();
$config->setAllowedTypes(['string']);

echo $config->process('There is a string'); // will return the acceptable input 'There is a string'

$config->process(12); // will throw an exception

作为复合嵌套元素,可以向数组节点添加子元素。

$config = new Configuration();
$config->addAllowedType('array');

$config->addChild('balance', new Configuration());
$config->getChild('balance')->addAllowedType('int');

$config->process(['balance' => 12]);

验证

可以为每个元素添加一个验证列表。此验证可以执行多个操作

  • 更改元素值
  • 从父元素中删除元素

通过这种方式,验证必须返回元素值。

验证可以是一个回调或实现 Kairos\Config\Validation\ValidationInterface 的对象实例。

$config = new Configuration();

$config->addValidation(function($element){
	return is_array($element) ? $element : [$element];
});

使用对象

class EmbedAsArray implements ValidationInterface
{
    /**
     * Validate
     *
     * Validate the element
     *
     * @param mixed $element The element to validate
     *
     * @return mixed
     */
    public function validate($element)
    {
    	return is_array($element) ? $element : [$element];
    }
}

必需元素和默认值

元素可以被设置为必需元素以确保其在结果中的存在。默认情况下,元素可以被跳过并且不会出现在结果中。

$config = new Configuration();
$config->setRequired(true);

还可以定义一个默认值。

$config = new Configuration();
$config->setDefaultValue('default_value');
$config->setDefaultValue(null);

后处理验证

为了与处理结果的完整结果交互,元素可以定义一个后验证。此验证必须是实现 PostValidationInterface 的对象。

class ConflictWithAlt implements PostValidationInterface
{
	private $originalNode;

    /**
     * Validate
     *
     * Validate the element
     *
     * @param mixed $element The element to validate
     *
     * @return mixed
     */
    public function validate($element)
    {
    	if (isset($element['alt'])) {
    		throw new LogicException('Something as a conflict with "alt" element');
    	}
    	return $element;
    }
    
    /**
     * Set original node
     *
     * Set up the original validation node
     *
     * @param Configuration $node The original node
     *
     * @return $this
     */
    public function setOriginalNode(Configuration $node) : PostValidationInterface
    {
    	$this->originalNode = $node;
    	return $this;
    }
}
$config = new Configuration();
$config->addPostValidation(new ConflictWithAlt());

预定义元素

节点

存在一些预定义的节点。这些节点是具有预定义允许类型或装饰逻辑的 Configuration 元素。

根节点

树的第一个节点,存储后验证并执行它们。扩展 ArrayNode

数组节点

允许数组类型,可以是原型。

标量节点

允许整数、字符串、浮点数、双精度浮点数和布尔值。

整数节点

允许整数值。

字符串节点

允许字符串值。

验证

存在一些预定义的验证。这些验证是 ValidationInterface 的实现。

允许值

验证给定值是否允许。

$config = new StringNode();
$config->addValidation(new AllowedValue('mysql', 'oracle'));

$config->process('mysql'); // Work
$config->process('oracle'); // Work
$config->process('mongodb'); // Fail
可激活

将布尔值 'true' 转换为其他值。注意 'bool' 类型必须允许

$config = new ArrayNode();
$config->addAllowedType('bool');
$config->addValidation(new IsActivable(['debug' => true, 'env' => 'test']));

$config->process(['debug' => true, 'env' => 'dev']); // Will return the input
$config->process(true); // Will return the IsActivable constructor argument
可禁用

通过删除当前键来转换布尔值 'false'。

$root = new RootNode();

$config = new ArrayNode();
$config->addAllowedType('bool');
$config->addValidation(new IsDisableable());

$root->addChild('manager', $config);

$root->process(['manager' => ['a', 'b', 'c']]); // Will return the input
$root->process(['manager' => false]); // Will return an empty array
类型

验证给定元素是否为允许的类型。与 IsActivable 或 IsDisableable 一起使用很有用

$config = new ArrayNode();
$config->addAllowedType('bool');
$config->addValidation(new IsActivable(['debug' => true, 'env' => 'test']));
$config->addValidation(new IsType(['array']));

$config->process(false); // Will fail due to type validation

原型

数组节点也可以是原型。区别在于原型将获取所有第一级元素并将子元素应用于每个部分。

$config = new ArrayNode();
$config->addChild('class', new StringNode());
$config->addChild('args', new ArrayNode());

$config->getChild('class')->setRequired(true);

$config->process([
	'user' => [
		'class' => 'MyClass'
	],
	'dispatcher' => [
		'class' => 'EventDispatcher',
		'args' => [1, 2, 3]
	]
]);