microlib/config

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A small, PHP, configuration library consisting mostly of functions.

0.1.1 2014-09-18 18:44 UTC

This package is auto-updated.

Last update: 2021-09-20 03:22:51 UTC


README

A small, PHP, configuration library consisting mostly of functions. Yep, functions, because working with configuration arrays should be simple and fast.

Features

  • Small library. No bloat. Easy-to-use functions.
  • Supports PHP array, JSON, INI, YAML, and XML config file formats.
  • Easy to apply defaults, enforce required settings, and filter out extraneous data.
  • Can specify a schema to recursively validate the data. Uses callables for validations and transformations.
  • Easily fetch values from nested config structure.

Usage

Validating Against a Schema

Let's say you have a config file that looks like this:

{
    "first_name": "Jeremy ",
    "last_name": "Lindblom",
    "phone": {"number": "5559997777"},
    "gender": "whocares"
}

In your PHP script…

Include the Config library.

require '/path/to/vendor/autoload.php';

use MicroLib\Config as cfg;

Load the config file.

$config = cfg\load('config.json');

Define a schema for the data. Using the schema function is optional but is useful for validating that the schema is defined correctly. (Note: The schema can be placed in a separate file and loaded with the load function like other config data.)

$schema = cfg\schema([
    'first_name' => [
        'required'  => true,
        'transform' => 'trim',
        'validate'  => 'ctype_alpha'
    ],
    'last_name' => [
        'required'  => true,
        'transform' => 'trim',
        'validate'  => 'ctype_alpha'
    ],
    'phone' => [
        'schema' => [
             'type' => [
                 'default'   => 'mobile',
                 'transform' => 'trim',
                 'validate'  => 'ctype_alpha',
             ],
             'number' => [
                 'required' => true,
                 'validate' => 'ctype_digit',
             ]
        ]
    ],
]);

Validate the configuration data with the schema. If no exception is thrown, then the data is valid.

$config = cfg\validate($config, $schema);

Fetch data from the config using get.

echo cfg\get($config, 'phone.number');
#> 5559997777

echo cfg\get($config, 'phone.type');
#> mobile

Simple Check for Required Keys

If you don't need the functionality of the schemas, there is still an easy way to enforce required settings and apply default values.

Let's say you have the following array of configuration data:

$config = [
    'class'  => 'rogue',
    'race'   => 'half-elf',
    'level'  => 5,
    'weapon' => 'short sword',
];

If you aren't concerned about all of the items, you can use the keep function to create a new array of only the items you want.

$config = cfg\keep($config, ['class', 'race', 'level']);

Then, using the create function, you can check requirements and apply defaults.

$config = cfg\create(
	$config,
	['class', 'race', 'level'],
	['status' => 'normal']
);

print_r($config);
#> Array
#> (
#>     [class] => rogue
#>     [race] => half-elf
#>     [level] => 5
#>     [status] => normal
#> )