didix16 / php-apidataobject
A simple library that allows easy handle incoming data from any sources (specially from API sources) and put data in a fashion way which is more practical to access it like an array or object. It allows to implement a DTO very fast.
Installs: 7 203
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >= 7.2
README
A simple library that allows easy handle incoming data from any sources (specially from API sources) and put data in a fashion way which is more practical to access it like an array or object. It allows to implement a DTO very fast.
Content
What is an ApiDataObject
It is just a class that can handle incoming data from API sources in JSON format or any other format and structure them in memory to easy handling like an associative array and/or general object.
To pass data into this class, first it should be a simple PHP associative array or plain PHP std object.
It can be created by using JSON string text.
Installation
composer require didix16/php-apidataobject
Usage
class ApiPlatformDataObject extends ApiDataObject {} ... class AWebHookProcessor { /** * @var array|object */ $data; public function process() { $apiData = new ApiPlatformDataObject($this->data); /** * data = * [ * 'property1' => 'value1', * 'property2' => 'value2', * 'property3' => 'value3', * 'property4' => 'value4', * ... * ] */ /** * Different accessors */ $apiData['property1']; $apiData->property1; $apiData->property1(); /** * Different setters */ $apiData['property1'] = 'value5'; $apiData->property1 = 'value5'; // chainable setter properties $apiData ->property1('value5') ->property2('value6') ... } public function processJson(string $json) { $apiData = ApiPlatformDataObject::fromJson($json); /** * Different accessors */ $apiData['property1']; $apiData->property1; $apiData->property1(); /** * Different setters */ $apiData['property1'] = 'value5'; $apiData->property1 = 'value5'; // chainable setter properties $apiData ->property1('value5') ->property2('value6') ... } }
See also
- php-apidatamapper - A DTO library that allows map incoming API data to any of your entities/models by using a simple filed mapping language.