Download the PHP package mr-luke/configuration without Composer
On this page you can find all versions of the php package mr-luke/configuration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mr-luke/configuration
More information about mr-luke/configuration
Files in mr-luke/configuration
Package configuration
Short Description Dot notation array host configuration package.
License MIT
Informations about the package configuration
Configuration Host
This package provides array host (wrapper) package that supports dot notation access and schema validation.
- Getting Started
- Installation
- Usage
- Plans
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
Or use the following command
Usage
Let's move to a Schema
class. It's a validation tool with one interface method:
$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:
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
:
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.
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:
Your key
can follow dot notation to access nested keys
:
By default if key
is not present, Host
returns null
. You can also use magic getter to acces config:
You can check if given hey is present:
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
All versions of configuration with dependencies
ext-json Version *