innmind / config
Config schema validator
Requires
- php: ~7.2
- innmind/immutable: ^2.8
Requires (Dev)
- giorgiosironi/eris: ^0.9.0
- phpunit/phpunit: ~6.0
- symfony/yaml: ^4.0
This package is auto-updated.
Last update: 2022-02-01 13:12:49 UTC
README
master |
develop |
---|---|
Library to enforce a configuration schema with an emphasis on schema visualization.
It's born after a constant struggle to build prototype definitions with symfony/config
as well as the fact prototypes can't be mixed with property definitions.
Note: a prototype is a definition allowing to repeat the sub-schema in the array, for example prototype<int>: bool
allows you to have a configuration like [1 => true, 2 => false /* etc... */]
.
Installation
composer require innmind/config
Usage
To better visualize the config schema you should define it via yaml or json (or any other minimalist format). Here's an example showing the possibilities of this library:
# schema.yml prototype<string>: type: string alias: '?string' repository: '?string' factory: '?string' labels: set<string> identity: property: string type: string properties: prototype<string>: type: string prototype<scalar>: mixed
use Innmind\Config\Config; use Symfony\Component\Yaml\Yaml; $config = (new Config)->build(Yaml::parseFile('schema.yml')); $data = $config->process($data);
When you call process
it will validate the $data
but also transform the data in case of set
, stream
and sequence
types by returning instances of Innmind\Immutable\Set
, Innmind\Immutable\Stream
and Innmind\Immutable\Sequence
, the $data
returned is also a Innmind\Immutable\Map
instead of a simple array in order to ease the manipulation of the config data.
The full list of keys formats you can use:
prototype<int>
will instruct to have an int as a keyprototype<int>+
will instruct to have an int as a key and to have at least one keyprototype<string>
will instruct to have an string as a keyprototype<string>+
will instruct to have an string as a key and to have at least one keyprototype<scalar>
will instruct to have an scalar as a keyprototype<scalar>+
will instruct to have an scalar as a key and to have at least one key- any other string will be considered as a key name
The full list of values formats you can use:
- any value having a
is_{type}
function, this includesint
,float
,bool
,string
,scalar
,array
,object
andresource
, can be declared optional by prefixing with a?
set<{type}>
wheretype
is any value accepted byInnmind\Immutable\Set
set<{type}>+
wheretype
is any value accepted byInnmind\Immutable\Set
and must at least have one valuestream<{type}>
wheretype
is any value accepted byInnmind\Immutable\Stream
stream<{type}>+
wheretype
is any value accepted byInnmind\Immutable\Stream
and must at least have one valuesequence
sequence+
must have at least one valueenum({typeA|typeB})
possible values separated by a|
?enum({typeA|typeB})
possible values separated by a|
but the value is optional
Extend behaviour
The formatsshowed are the one by defaults but you can easily yours, for that you need to implements either Structure
or Property
then to use it:
$config = new Config( new Structures( ...Structures::defaults()->add(MyStructure::class) ), new Properties( ...Properties::defaults()->add(MyProperty::class) ) );