Model config
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
, (*1)
This library contains functionality to configure your models with predefined
configuration values, like enums. It also gives you a nice object-oriented way
to handle these., (*2)
Requirements
- PHP >= 5.5
- The stemmer extension: https://github.com/jbboehr/php-stemmer
Installation
composer require treehouselabs/model-config
Usage
First, you need to define field configurations. Let's say you have a house model, with a type:, (*3)
# Project\Model\Article
class House
{
protected $type;
}
The available types are "house", "apartment" and "other". Now, we could introduce an HouseType
entity,
but that would mostly contain a name, and nothing more. This is where an enum comes in handy. Enums are
classes that have constants pointing to available values. We can create an enum for our type like this:, (*4)
# Project\Model\Config\Field\HouseType
use TreeHouse\Model\Config\Field\Enum;
class HouseType extends Enum
{
const HOUSE = 1;
const APARTMENT = 2;
const OTHER = 3;
}
Now you can use these constants to set values and make it readable too:, (*5)
$house = new House();
$house->setType(HouseType::APARTMENT);
Multiple values
Enums can also be denoted as multivalued fields. For example, our house model could have some facilities:, (*6)
# Project\Model\Config\Field\Facilities
use TreeHouse\Model\Config\Field\Enum;
class Facilities extends Enum
{
const ELEVATOR = 1;
const ALARM = 2;
const AIRCONDITIONING = 3;
const ROLLER_BLINDS = 4;
protected static $multiValued = true;
}
Notice how we defined this configuration to be multivalued. The enum itself doesn't do anything with this
information. But it comes in useful in other places, which we'll talk about next., (*7)
Configuration
Now that we have a couple of configurations, we can bundle them in a config object. The config object
uses the (lowercased) constant names mapped to their values. We can use a builder to do this:, (*8)
$builder = new ConfigBuilder();
$builder->addField('type', HouseType::class);
$builder->addField('facilities', Facilities::class);
$config = $builder->getConfig();
The config object provides some convenience methods:, (*9)
$config->isMultiValued('facilities'); // true
$config->hasFieldConfig('foo'); // false
$config->hasFieldConfigKey('type', 2); // true
$config->hasFieldConfigValue('type', 'apartment'); // true
$config->getFieldConfigValueByKey('type', 2); // 'apartment'
$config->getFieldConfigKey('type', 'apartment'); // 2
Testing
bash
composer test
, (*10)
Security
If you discover any security related issues, please email dev@treehouse.nl instead of using the issue tracker., (*11)
License
The MIT License (MIT). Please see License File for more information., (*12)
Credits