new-inventor / property-bag
具有自动值归一化和格式化的属性包
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: 2024-09-15 02:30:26 UTC
README
此实用工具为您提供
- 基本属性包类
- 属性包接口
- 属性包配置
- 属性包元数据类
- 从配置生成类的生成命令
安装
composer require new-inventor/property-bag
PropertyBag 使用
属性包创建
- 创建属性包配置。
- 运行
php bin/console generate:bag <配置路径> <目标路径> [--base-namespace="Base\Namespase"] [-f(to force rewrite destination files)]
- 检查目标文件夹中是否存在文件
- 如果您愿意,您可以在注释
CustomCodeBegin
和CustomCodeEnd
之间编写一些代码
无配置的独立对象
$propertyBag = new PropertyBag(); $propertyBag->add('time', new \DateTime()); $propertyBag->add('value'); $propertyBag->get('time'); $propertyBag->set('value', 123); $propertyBag->get('value');
具有配置的对象
配置文件
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
位置
- 父类(根) - 父类。如果未指定,则默认为
NewInventor\PropertyBag\PropertyBag
- 抽象类(根) - 生成类是否为抽象类
- 验证(根) - symfony 验证配置
- 属性(根) - 属性列表
- 验证(在属性中) - 类获取器的 symfony 验证
- 转换器(在属性中) - 从 [https://github.com/new-inventor/transformers] 或您代码中的完整类名中提取的短类名
- 嵌套(在属性中) - 属性是嵌套类,因此必须提供类,并提供数组键(为数组)
- 默认(在属性中) - 属性的默认值
- 获取器(根) - 可以为布尔值或对象。哪些获取器将在类中生成。
- 设置器(根) - 可以为布尔值或对象。哪些设置器将在类中生成。
在转换器中,您可以指定组、转换器参数。在 InnerTransformer
和 ChainTransformer
中,您必须将其他转换器作为参数传递。
如果您未指定组,则默认为 'default'。
您可以将标量、数组、可调用函数以及具有单个元素的特数组(const、static)传递给参数。
用法
//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