dev-master
9999999-devA processor system for configuration structure
MIT
The Development Requires
- phpunit/phpunit ^7
- infection/infection ^0.8
- phpmd/phpmd ^2.6
- phpmetrics/phpmetrics ^2.3
- sebastian/phpcpd ^4.0
- squizlabs/php_codesniffer ^3.2
A processor system for configuration structure
A processor for configuration structure, (*1)
The key concept of the configuration is to append elements inside another, following composite structure pattern., (*2)
Each configuration element will specify a list of allowed type inside., (*3)
$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
As composite nested elements, it's possible to add childs to an array node., (*4)
$config = new Configuration(); $config->addAllowedType('array'); $config->addChild('balance', new Configuration()); $config->getChild('balance')->addAllowedType('int'); $config->process(['balance' => 12]);
It's possible to add a list of validation for each elements. This validation can execute more than one action :, (*5)
By this way, the validation MUST return the element value., (*6)
The validation can be a callback or a n instance of object implementing the Kairos\Config\Validation\ValidationInterface
., (*7)
$config = new Configuration(); $config->addValidation(function($element){ return is_array($element) ? $element : [$element]; });
With object :, (*8)
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]; } }
The element can be set as required element to ensure the presence in the result. By default, the element can be skipped and will not be present in the result., (*9)
$config = new Configuration(); $config->setRequired(true);
It's also possible to define a default value., (*10)
$config = new Configuration(); $config->setDefaultValue('default_value'); $config->setDefaultValue(null);
To interact with the complete result of the process, the element can define a post validation. This validation must be an object implementing the PostValidationInterface., (*11)
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());
Some predefined nodes exists. These nodes are Configuration
elements, with predefined allowed types, or decoration logic., (*12)
The first node of a tree, store the PostValidations and execute them. Extends ArrayNode
., (*13)
Allow array type, can be a prototype., (*14)
Allow integer, string, float, double and boolean value., (*15)
Allow integer value., (*16)
Allow string value., (*17)
Some predefined validations exists. These validations are ValidationInterface
implementations., (*18)
Validate that the given value is allowed., (*19)
$config = new StringNode(); $config->addValidation(new AllowedValue('mysql', 'oracle')); $config->process('mysql'); // Work $config->process('oracle'); // Work $config->process('mongodb'); // Fail
Transform a boolean 'true' value to something. Note 'bool' type must be allowed, (*20)
$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
Transform a boolean 'false' value by removing the current key., (*21)
$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
Validate the given element as allowed type. Usefull with concordance of IsActivable or IsDisableable, (*22)
$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
An array node can also be a prototype. The difference is that a prototype will take all first level elements and apply the childs to each parts., (*23)
$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] ] ]);
A processor system for configuration structure
MIT