akbsit/trait-adapter

特质帮助适应数据。

1.0.6 2024-04-06 12:14 UTC

This package is auto-updated.

Last update: 2024-09-06 13:12:22 UTC


README

安装

要安装包,您需要运行以下命令

composer require akbsit/trait-adapter

用法

要创建适配器类,必须在开始时连接特质 AdapterTrait。接下来,定义属性 protected array $arMappingList,其中包含字段对应关系(可以是 = [];)。

setAttribute() 方法必须是 protected 并返回 void

示例对象初始化

$oExampleAdapter = ExampleAdapter::make();
$oExampleAdapter = new ExampleAdapter();

示例

  1. 包装适配器对象
<?php namespace App\Classes;

use Akbsit\TraitAdapter\AdapterTrait;

/**
 * Class ExampleAdapter
 * @package App\Classes
 */
class ExampleAdapter
{
    use AdapterTrait;

    protected array $arMappingList = [
        'id',
        'name',
    ];

    /* @var int */
    public $id;

    /* @var string */
    public $name;
}

将数据转换为适配器对象

$oExampleAdapter = ExampleAdapter::make()
    ->create([
        'id'   => 10,
        'name' => 'string',
    ]);
App\Classes\ExampleAdapter {
  id: 10
  name: "string"
}
  1. 转换对象属性
<?php namespace App\Classes;

use Akbsit\TraitAdapter\AdapterTrait;

/**
 * Class ExampleAdapter
 * @package App\Classes
 */
class ExampleAdapter
{
    use AdapterTrait;

    protected array $arMappingList = [
        'id',
        'name',
    ];

    /* @var int */
    public $id;

    /* @var string */
    public $name;

    /* @return void */
    protected function setNameAttribute(): void
    {
        $this->name = $this->name . '_new_value';
    }
}

将数据转换为适配器对象

$oExampleAdapter = ExampleAdapter::make()
    ->create([
        'id'   => 10,
        'name' => 'string',
    ]);
App\Classes\ExampleAdapter {
  id: 10
  name: "string_new_value"
}
  1. 映射对象数据
<?php namespace App\Classes;

use Akbsit\TraitAdapter\AdapterTrait;

/**
 * Class ExampleAdapter
 * @package App\Classes
 */
class ExampleAdapter
{
    use AdapterTrait;

    protected array $arMappingList = [
        'id'   => 'external_id',
        'name' => 'external_name',
    ];

    /* @var int */
    public $id;

    /* @var string */
    public $name;
}

将数据转换为适配器对象

$oExampleAdapter = ExampleAdapter::make()
    ->create([
        'external_id'   => 13,
        'external_name' => 'external',
    ]);
App\Classes\ExampleAdapter {
  id: 13
  name: "external"
}
  1. 创建集合
<?php namespace App\Classes;

use Akbsit\TraitAdapter\AdapterTrait;

/**
 * Class ExampleAdapter
 * @package App\Classes
 */
class ExampleAdapter
{
    use AdapterTrait;

    protected array $arMappingList = [
        'id',
        'name',
    ];

    /* @var int */
    public $id;

    /* @var string */
    public $name;
}

将数据转换为适配器集合

$oExampleAdapter = ExampleAdapter::make()
    ->createCollection([
        ['id' => 1, 'name' => 'string_1'],
        ['id' => 2, 'name' => 'string_2'],
    ]);
App\Classes\ExampleAdapter {
  +"arCollection": array:2 [
    0 => App\Classes\ExampleAdapter {
      +id: 1
      +name: "string_1"
    }
    1 => App\Classes\ExampleAdapter {
      +id: 2
      +name: "string_2"
    }
  ]
}

方法

初始化方法

  • make() - 初始化对象;
  • mapping(array $arMappingList = []) - 定义匹配数组;
  • setCustom(array $arCustomData = []) - 将自定义数据传输到适配器类;
  • create(array $arData = []) - 创建适配器对象;
  • createCollection(array $arDataList = [], int $iChunk = 500) - 创建适配器集合;
  • toArray() - 转换为数组。

在适配器类内部

  • getOrigin(?string $sKey = null) - 获取适配器类中的原始值;
  • getCustom(?string $sKey = null) - 获取适配器类中的自定义值;
  • getCustomByItemIndex(?string $sKey = null) - 通过索引在适配器类中获取自定义值。