2017 © Pedro Peláez
 

library property-bag

Property bag with automatic value normalizing and formatting

image

new-inventor/property-bag

Property bag with automatic value normalizing and formatting

  • Wednesday, January 17, 2018
  • by George Ionov
  • Repository
  • 1 Watchers
  • 0 Stars
  • 367 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 53 Versions
  • 3 % Grown

The README.md

Property bag

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, (*1)

Installation

composer require new-inventor/property-bag, (*2)

PropertyBag usage

Property bag creation

  1. Create property bag configuration.
  2. run php bin/console generate:bag <configuration path> <destination path> [--base-namespace="Base\Namespase"] [-f(to force rewrite destination files)]
  3. check if file exist in destination folder
  4. if you want, you can write some code between comments CustomCodeBegin and CustomCodeEnd

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., (*3)

In transformers you can specify group, transformer parameters. In InnerTransformer and ChainTransformer you must pass as parameters another Transformers., (*4)

If you do not specify the group, then it will be 'default'., (*5)

You can pass to parameters scalars, arrays, callable, and special arrays with one element(const, static)., (*6)

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

The Versions

17/01 2018

dev-master

9999999-dev

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

17/01 2018

3.6.4

3.6.4.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

16/01 2018

3.6.3

3.6.3.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

09/01 2018

3.6.2

3.6.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

20/12 2017

3.6.1

3.6.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

18/12 2017

3.6.0

3.6.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

07/11 2017

3.5.0

3.5.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

02/11 2017

3.4.0

3.4.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

27/10 2017

3.3.9

3.3.9.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

27/10 2017

3.3.8

3.3.8.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

16/10 2017

3.3.7

3.3.7.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.3.6

3.3.6.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.3.5

3.3.5.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.3.3

3.3.3.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.3.4

3.3.4.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.3.2

3.3.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.3.1

3.3.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.3.0

3.3.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.2.3

3.2.3.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

13/10 2017

3.2.2

3.2.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

12/10 2017

3.2.1

3.2.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

12/10 2017

3.2.0

3.2.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2017

3.1.5

3.1.5.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2017

3.1.4

3.1.4.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2017

3.1.3

3.1.3.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2017

3.1.2

3.1.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2017

3.1.1

3.1.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

11/10 2017

3.1.0

3.1.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

10/10 2017

3.0.2

3.0.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

10/10 2017

3.0.1

3.0.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

02/10 2017

2.1.2

2.1.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

02/10 2017

2.1.1

2.1.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

02/10 2017

2.1.0

2.1.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2017

2.0.4

2.0.4.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2017

2.0.3

2.0.3.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2017

2.0.2

2.0.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2017

2.0.1

2.0.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2017

2.0.0

2.0.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

30/09 2017

1.7.0

1.7.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

29/09 2017

1.6.3

1.6.3.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2017

1.6.2

1.6.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2017

1.6.1

1.6.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2017

1.6.0

1.6.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2017

1.5.0

1.5.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2017

1.4.2

1.4.2.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2017

1.4.1

1.4.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

28/09 2017

1.4.0

1.4.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

27/09 2017

1.3.0

1.3.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

26/09 2017

1.2.0

1.2.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

26/09 2017

1.1.1

1.1.1.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

26/09 2017

1.1.0

1.1.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires

18/09 2017

1.0.0

1.0.0.0

Property bag with automatic value normalizing and formatting

  Sources   Download

MIT

The Requires

 

The Development Requires