akbsit/trait-adapter
最新稳定版本:1.0.6
Composer 安装命令:
composer require akbsit/trait-adapter
包简介
Trait help to adapt data.
README 文档
README
Install
To install package, you need run command:
composer require akbsit/trait-adapter
Usage
To create Adapter class, it is necessary to connect trait at the beginning AdapterTrait. Next, define the property protected array $arMappingList, where are the field correspondences denoted (it can be = [];).
setAttribute() метод должен быть
protectedи возвращатьvoid
Examples object initialization
$oExampleAdapter = ExampleAdapter::make();
$oExampleAdapter = new ExampleAdapter();
Examples
- Wrapper Adapter object:
<?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; }
Converting data to Adapter object:
$oExampleAdapter = ExampleAdapter::make() ->create([ 'id' => 10, 'name' => 'string', ]);
App\Classes\ExampleAdapter {
id: 10
name: "string"
}
- Converting object properties:
<?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'; } }
Converting data to Adapter object:
$oExampleAdapter = ExampleAdapter::make() ->create([ 'id' => 10, 'name' => 'string', ]);
App\Classes\ExampleAdapter {
id: 10
name: "string_new_value"
}
- Mapping object data:
<?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; }
Converting data to Adapter object:
$oExampleAdapter = ExampleAdapter::make() ->create([ 'external_id' => 13, 'external_name' => 'external', ]);
App\Classes\ExampleAdapter {
id: 13
name: "external"
}
- Creating collection:
<?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; }
Converting data to the Adapter collection:
$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"
}
]
}
Methods
Initialization methods
make()- initializing object;mapping(array $arMappingList = [])- defining array of matches;setCustom(array $arCustomData = [])- transferring custom data to the Adapter class;create(array $arData = [])- create object Adapter;createCollection(array $arDataList = [], int $iChunk = 500)- create collection Adapter;toArray()- convert to an array.
Inside the Adapter class
getOrigin(?string $sKey = null)- get original value in the Adapter class;getCustom(?string $sKey = null)- get a custom value in the Adapter class;getCustomByItemIndex(?string $sKey = null)- get a custom value by index in the Adapter class.
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-16