trait-adapter, Packagist
To install package, you need run command:
composer require akbsit/trait-adapter
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
$oExampleAdapter = ExampleAdapter::make();
$oExampleAdapter = new ExampleAdapter();
- 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"
}
]
}
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.
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.