liberty_code/handle_model

v1.0.0 2024-08-17 17:45 UTC

This package is auto-updated.

Last update: 2024-09-17 18:03:12 UTC


README

描述

该库包含处理模型组件,用于处理模型组件。

要求

  • 脚本语言:PHP:版本 7 或 8

框架库实现要求

  1. 库仓库:liberty_code/validation:版本 1.0

    • 标准规则实现

      默认属性规范提供实体属性规则配置,兼容验证器,包含所有标准规则,添加到其集合规则集合。

    • 集合规则集合实现

      默认属性规范提供实体属性规则配置,兼容验证器,使用集合规则集合,要求其规则配置格式。

安装

有多种可能的方式

Composer

  1. 要求

    它需要Composer安装。更多信息: https://getcomposer.org.cn

  2. 命令:在项目根路径下移动

     cd "<project_root_path>"
    
  3. 命令:安装

     php composer.phar require liberty_code/handle_model ["<version>"]
    
  4. 注意

    • 包含供应商

      如果项目使用Composer,供应商必须被包含

        require_once('<project_root_path>/vendor/autoload.php');
      
    • 配置

      安装命令允许在Composer文件中添加

        {
            "require": {
                "liberty_code/handle_model": "<version>"
            }
        }
      

包含

  1. 下载

    • 下载以下仓库。
    • 将其放在仓库根路径上。
  2. 包含源代码

     require_once('<repository_root_path>/include/Include.php');
    

用法

属性规范数据类型

数据类型表示特定类型,允许以特定方式管理实体属性值。

元素

  • DataType

    允许设计特定类型,以管理实体属性值(验证、格式化)。

  • StringDataType

    扩展数据类型功能。允许管理实体属性字符串值。

  • NumericDataType

    扩展数据类型功能。允许管理实体属性数值。

  • BooleanDataType

    扩展数据类型功能。允许管理实体属性布尔值。

  • DateDataType

    扩展数据类型功能。允许管理实体属性日期值。

  • DataTypeCollection

    允许设计数据类型集合。允许从指定数据类型列表中检索指定数据类型。

  • DataTypeFactory

    允许设计数据类型工厂,从指定配置提供新或指定数据类型实例。

  • BaseDataTypeFactory

    扩展数据类型工厂功能。可以作为所有数据类型工厂的基础。

  • StandardDataTypeFactory

    扩展基础数据类型工厂功能。提供数据类型实例。

  • DataTypeBuilder

    使用源数据数组来填充数据类型集合。

示例

// Get data type factory
use liberty_code\model\datetime\factory\model\DefaultDateTimeFactory;
use liberty_code\handle_model\attribute\specification\type\factory\standard\model\StandardDataTypeFactory;
$dataTypeFactory = new StandardDataTypeFactory(
    new DefaultDateTimeFactory(array(...))
);
...
// Get new data type, from configuration
$dataType = $dataTypeFactory->getObjDataType(array(...));
...
// Check matches with specific type
var_dump($dataType->checkMatches(...));
...
// Get entity attribute rule configuration
var_dump($dataType->getTabRuleConfig());
...
// Get entity attribute formatted value
var_dump($dataType->getValueFormatGet(...));
var_dump($dataType->getValueFormatSet(...));
var_dump($dataType->getValueSaveFormatGet(...));
var_dump($dataType->getValueSaveFormatSet(...));
...
// Get data type collection
use liberty_code\handle_model\attribute\specification\type\model\DefaultDataTypeCollection;
$dataTypeCollection = new DefaultDataTypeCollection();
...
// Get data type builder
use liberty_code\handle_model\attribute\specification\type\build\model\DefaultDataTypeBuilder;
$dataTypeBuilder = new DefaultDataTypeBuilder($dataTypeFactory);
...
// Hydrate data type collection
$dataTypeBuilder->setTabDataSrc(array(...));
$dataTypeBuilder->hydrateDataTypeCollection($dataTypeCollection);
...
foreach($dataTypeCollection->getTabKey() as $key) {
    echo($dataTypeCollection->getObjDataType($key)->getStrKey() .'<br />');
}
/**
 * Show: 
 * Data type key 1
 * ...
 * Data type key N
 */
...

属性规范

属性规范允许定义特定数据类型的范围,以管理实体属性值。

元素

  • AttrSpec

    允许提供数据类型的范围,以管理实体属性值(验证、格式化)。

  • StandardAttrSpec

    扩展属性规范功能。使用数据类型对象来管理实体属性值。

示例

// Get attribute specification
use liberty_code\handle_model\attribute\specification\standard\model\StandardAttrSpec;
$attrSpec = new StandardAttrSpec($dataTypeCollection, array(...));
...
// Get array of available data types
var_dump($attrSpec->getTabDataType());
...
// Get entity attribute rule configuration, from specific data type
$dataType = '...';
var_dump($attrSpec->getTabDataTypeRuleConfig($dataType));
...
// Get entity attribute formatted value, from specific data type
var_dump($attrSpec->getDataTypeValueFormatGet($dataType, ...));
var_dump($attrSpec->getDataTypeValueFormatSet($dataType, ...));
var_dump($attrSpec->getDataTypeValueSaveFormatGet($dataType, ...));
var_dump($attrSpec->getDataTypeValueSaveFormatSet($dataType, ...));
...

属性

属性包含用于设计特定属性(可在配置的实体中使用)的配置。

元素

  • 属性

    包含用于在实体上设计特定属性的配置。

  • AttributeCollection

    允许设计属性集合。允许提供实体属性配置。

  • SaveAttribute

    扩展属性功能。可以在保存实体时使用。

  • SaveAttributeCollection

    扩展属性集合功能。允许提供保存实体属性配置。

  • AttributeFactory

    允许设计属性工厂,从指定的配置中提供新的或特定的属性实例。

  • StandardAttributeFactory

    扩展属性工厂功能。提供属性实例。

  • Builder

    Builder 允许使用属性填充属性集合。

示例

// Get attribute factory
use liberty_code\handle_model\attribute\factory\standard\model\StandardAttributeFactory;
$attributeFactory = new StandardAttributeFactory(null, $provider, $attrSpec);
...
// Get attribute builder
use liberty_code\handle_model\attribute\build\model\DefaultBuilder;
$attributeBuilder = new DefaultBuilder($attributeFactory);
...
// Get attribute collection
use liberty_code\handle_model\attribute\repository\model\DefaultSaveAttributeCollection;
$attributeCollection = new DefaultSaveAttributeCollection();
...
// Hydrate attribute collection
$attributeBuilder->setTabDataSrc(array(...));
$attributeBuilder->hydrateAttributeCollection($attributeCollection);
...
foreach($attributeCollection->getTabAttributeKey() as $attributeKey) {
    echo($attributeCollection->getObjAttribute($attributeKey)->getStrAttributeKey() .'<br />');
}
/**
 * Show: 
 * Attribute key 1
 * ...
 * Attribute key N
 */
...

属性提供者

属性提供者允许为实体提供属性信息。

元素

  • AttrProvider

    允许为实体提供属性信息。

  • StandardAttrProvider

    扩展属性提供者功能。使用属性对象来为实体提供属性信息。

示例

use liberty_code\handle_model\attribute\provider\standard\model\StandardAttrProvider;
$attrProvider = new StandardAttrProvider($attributeCollection);
...
// Get entity attribute configuration array
var_dump($attrProvider->getTabEntityAttrConfig());
...
// Get entity attribute rule configurations array
var_dump($attrProvider->getTabEntityAttrRuleConfig());
...

实体

使用属性提供者处理配置的实体,以构建和管理其属性。

元素

  • HandleValidatorConfigEntity

    扩展验证配置实体功能。使用属性提供者来构建和管理其属性。

  • HandleSaveConfigEntity

    扩展保存配置实体功能。使用属性提供者来构建和管理其属性。

示例

...
// Get new entity
use liberty_code\handle_model\entity\model\HandleValidatorConfigEntity
$handleValidatorConfigEntity = new HandleValidatorConfigEntity(
    $attrProvider,
    array(),
    $validator
);
...
if($handleValidatorConfigEntity->checkAttributeValid()) {
    $tabAttributeKey = $handleValidatorConfigEntity->getTabAttributeKey();
    foreach($tabAttributeKey as $attributeKey) {
        echo($handleValidatorConfigEntity->$attributeKey . '<br />');
    }
}
/**
 * Show: 
 * Entity attribute 1 value
 * ...
 * Entity attribute N value
 */
...

测试

单元测试

单元测试允许测试组件功能,并自动化它们的验证。

  1. 要求

  2. 命令:运行单元测试

     vendor\bin\phpunit
    
  3. 注意

    它使用 PHPUnit 处理单元测试。更多信息请访问:https://phpunit.readthedocs.io