falbar / trait-adapter
This package is abandoned and no longer maintained.
The author suggests using the akbsit/trait-adapter package instead.
Trait help to adapt data.
1.0.6
2024-04-06 12:14 UTC
Requires
- php: ^8.1
- ext-json: *
- dusank/knapsack: ^10.0
- illuminate/container: ^9.0|^10.0|^11.0
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.