new-inventor / property-bag
Property bag with automatic value normalizing and formatting
3.6.4
2018-01-17 14:58 UTC
Requires
- php: ^7.1
- new-inventor/data-structure: ^3.0
- new-inventor/transformers: ^1.0
Requires (Dev)
- codeception/codeception: 2.3.*
- phpunit/phpunit: ^5.7
- symfony/filesystem: 3.3.*
Suggests
- symfony/console: 3.3.*
- symfony/filesystem: 3.3.*
- dev-master
- 3.6.4
- 3.6.3
- 3.6.2
- 3.6.1
- 3.6.0
- 3.5.0
- 3.4.0
- 3.3.9
- 3.3.8
- 3.3.7
- 3.3.6
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.2
- 3.0.1
- 3.0.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.7.0
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
This package is not auto-updated.
Last update: 2025-01-19 03:59:29 UTC
README
This utility provide to you:
- base property bag class
- property bab interface
- configuration for property bag
- metadata classes for property bag
- generator command to generate classes from configuration
Installation
composer require new-inventor/property-bag
PropertyBag usage
Property bag creation
- Create property bag configuration.
- run
php bin/console generate:bag <configuration path> <destination path> [--base-namespace="Base\Namespase"] [-f(to force rewrite destination files)]
- check if file exist in destination folder
- if you want, you can write some code between comments
CustomCodeBegin
andCustomCodeEnd
Stand alone object with no configuration
$propertyBag = new PropertyBag(); $propertyBag->add('time', new \DateTime()); $propertyBag->add('value'); $propertyBag->get('time'); $propertyBag->set('value', 123); $propertyBag->get('value');
Object with configuration
Configuration file
parent: Some1\Some2\Some3\Parent abstract: true validation: constraints: - Callback: ['TestsDataStructure\TestStatic', 'GetTrue'] getters: prop1: - GreaterThan: value: 0 message: "Field 'prop1' must be greater than {{ compared_value }}" properties: prop0: - GreaterThan: 0 properties: prop1: NewInventor\Transformers\Transformer\ToInt prop2: transformers: - ToInt: ~ validation: - GreaterThan: value: 5 message: "Field 'prop2' must be > {{ compared_value }}" - LessThanOrEqual: value: 1000 message: "Field 'prop2' must be <= {{ compared_value }}" prop3: transformers: - ToBool: - ['TestsDataStructure\TestStatic', 'GetTrue'] prop4: transformers: - ToBool: - groups: forward - BoolToMixed: - static: ['TestsDataStructure\TestStatic', 'bbb'] - const: ['TestsDataStructure\TestStatic', 'AAA'] - groups: backward prop5: transformers: - ToBool: - groups: forward - BoolToMixed: - 1 - 0 - groups: backward prop6: transformers: - ToString: - groups: forward - CsvStringToArray: - groups: forward - InnerTransformer: - groups: forward - ToInt: ~ - ArrayToCsvString: - groups: backward prop7: default: 2222 transformers: - ToString: ~ - StringToDateTime: - 'd.m.Y' - groups: forward prop8: nested: class: TestsDataStructure\TestBag2 array: true prop9: nested: class: TestsDataStructure\TestBag1 getters: generate: true except: - prop0 - prop1 setters: generate: true only: - prop0
Where:
- parent(root) - parent class. If does not specified, then
NewInventor\PropertyBag\PropertyBag
- abstract(root) - is generated class abstract
- validation(root) - symfony validation config
- properties(root) - list of properties
- validation(in property) - symfony validation for class getter
- transformers(in property) - short class names from [https://github.com/new-inventor/transformers] or full class name from you code
- nested(in property) - property is nested class so it must provide class and can provide array key(to be an array)
- default(in property) - default value of property
- getters(root) - can be bool or object. Which getters generate in class.
- setters(root) - can be bool or object. Which setters generate in class.
In transformers you can specify group, transformer parameters. In InnerTransformer
and ChainTransformer
you must pass as parameters another Transformers.
If you do not specify the group, then it will be 'default'.
You can pass to parameters scalars, arrays, callable, and special arrays with one element(const, static).
Usage
//We use parser to parse configuration files into array. //Configuration is a description of configuration fields $parser = new NewInventor\DataStructure\Configuration\Parser\Yaml(new NewInventor\PropertyBag\Configuration\Configuration()) // MetadataLoader constructs the metadata object with transformers parameters and nested elements. It loads config from path by class name and removes from class name BaseNamespace $metadataLoader = new NewInventor\PropertyBag\Metadata\Loader('/path/To/Generated/Bags', $parser, 'Base\Namespace'); //Factory is the main class that loads Metadata and caches it. $metadataFactory = new NewInventor\PropertyBag\Metadata\Factory($loader, Psr\Cache\CacheItemPoolInterface); // Validation loader is the wrapper for Symfony metadata loader. it parses config file and loads class validation $validationLoader = new NewInventor\DataStructure\Validation\Loader('/path/To/Generated/Bags', $parser, 'Base\Namespace'); // Factory is the class that construct validator object. $validationFactory = new NewInventor\DataStructure\Validation\Factory($loader, Symfony\Component\Validator\Mapping\Cache\CacheInterface); // TestBag class is generated(or Written) propertyBag $bag = new TestBag(); $values = [1, 'qwe', 4, 'qweqw']; $metadataLoader = new NewInventor\PropertyBag\Metadata\Loader('/path/To/Generated/Bags', $parser, 'Base\Namespace'); $metadataFactory = new NewInventor\PropertyBag\Metadata\Factory($loader, Psr\Cache\CacheItemPoolInterface); //transformer make transformation on arrays and prepare data to loading to the object $transformer = $metadataFactory->getMetadataFor(TestBag::class)->getTransformer('default'); $values = $transfirmer->transform($values); //method loads values to the object from array $bag->load($values); $validationLoader = new NewInventor\DataStructure\Validation\Loader('/path/To/Generated/Bags', $parser, 'Base\Namespace'); $validationFactory = new NewInventor\DataStructure\Validation\Factory($loader, Symfony\Component\Validator\Mapping\Cache\CacheInterface); //If validation fails then $errors will be a not empty array otherwise it will be an empty array $errors = $validationFactory->getValidator()->validate($bag)->getErrors(); //after this preparations object will be clear? walid and ready to manipulations on it. ... do some staff