treehouselabs / model-config
Library to configure enums in models
Installs: 70 287
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 6
Forks: 0
Open Issues: 0
Requires
- php: >=5.5
- ext-stemmer: *
Requires (Dev)
- phpunit/phpunit: ~4.0
- symfony/validator: ~2.4
This package is auto-updated.
Last update: 2024-10-29 04:41:53 UTC
README
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.
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:
# 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:
# 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:
$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:
# 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.
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:
$builder = new ConfigBuilder(); $builder->addField('type', HouseType::class); $builder->addField('facilities', Facilities::class); $config = $builder->getConfig();
The config object provides some convenience methods:
$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
composer test
Security
If you discover any security related issues, please email dev@treehouse.nl instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.