innmind/config

This package is abandoned and no longer maintained. No replacement package was suggested.

Config schema validator

1.1.0 2018-04-02 08:32 UTC

This package is auto-updated.

Last update: 2022-02-01 13:12:49 UTC


README

master develop
Scrutinizer Code Quality Scrutinizer Code Quality
Code Coverage Code Coverage
Build Status Build Status

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 key
  • prototype<int>+ will instruct to have an int as a key and to have at least one key
  • prototype<string> will instruct to have an string as a key
  • prototype<string>+ will instruct to have an string as a key and to have at least one key
  • prototype<scalar> will instruct to have an scalar as a key
  • prototype<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 includes int, float, bool, string, scalar, array, object and resource, can be declared optional by prefixing with a ?
  • set<{type}> where type is any value accepted by Innmind\Immutable\Set
  • set<{type}>+ where type is any value accepted by Innmind\Immutable\Set and must at least have one value
  • stream<{type}> where type is any value accepted by Innmind\Immutable\Stream
  • stream<{type}>+ where type is any value accepted by Innmind\Immutable\Stream and must at least have one value
  • sequence
  • sequence+ must have at least one value
  • enum({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)
    )
);