mr-luke / configuration
Dot notation array host configuration package.
Installs: 437 123
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/mr-luke/configuration
Requires
- php: >=7.1
- ext-json: *
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
README
This package provides array host (wrapper) package that supports dot notation access and schema validation.
Getting Started
Good software development follows many patterns and architectures. We design things that depends on many other parts. Some of them are well structured Objects but many times we need to have some configurations. Often we use array as our config host but it can produce mass of unexpected side-effects becasue of one reason - array is not an Object so it can't follow any schema. But what if it can...
During my work I developed a simple wrapper tool that helped me with schema sensitive arrays and I decided to make it a package. I hope you enjoy it!
Installation
To install through composer, simply put the following in your composer.json file and run composer update
{
"require": {
"mr-luke/configuration": "~1.0"
}
}
Or use the following command
composer require "mr-luke/configuration"
Usage
Let's move to a Schema class. It's a validation tool with one interface method:
public function check(array $insert, bool $throw = true): bool
$insert- This is your array that is a subject of validation$throw- This option change behavior of validation
By default check method throws an InvalidArgumentException when $insert doesn't follow schema.
Step One
Create your Schema array:
$instruction = [ 'first_key' => 'required|string', 'second_key' => 'nullable|integer', 'third_key' => 'required|float', ];
Available rules:
required- given key must not be emptynullable- given key can be nullboolean- given key must be boolean typefloat- given key must be float typeinteger- given key must be integer typestring- given key must be string and can't be other types
Step Two
Create new instance of Mrluke\Configuration\Schema:
$schema = new Schema(array $instruction);
Note! From v1.2.0 you can create Schema by static method createFromFile(string $path, bool $json = false).
Step Three
Create new instance of Mrluke\Configuration\Host with Schema as a dependency and your $configArray is automatically validated.
$host = new Host($configArray, $schema);
If your $configArray doesn't follow given Schema, you will get InvalidArgumentException. You can also use Host without any Schema due to it's optional parameter of Mrluke\Configuration\Host.
Your configuration is Wrapped!
Now you have an access to Host methods:
/** * Return given key from array. * * @param string $key * @param mixed $default * @return mixed */ public function get(string $key, $default = null)
Your key can follow dot notation to access nested keys:
$host->get('mysql.database', 'my_db');
By default if key is not present, Host returns null. You can also use magic getter to acces config:
$host->mysql;
You can check if given hey is present:
/** * Determine if given key is present. * * @param string $key * @return bool */ public function has(string $key): bool
Plans
Feel free to contribute because I am aware that there are some things to improve. For now:
- Nested Schema support
- New validation rules support
- New Schema's file format