ag84ark / object-convertor
Array to object convert
Requires
- php: ^7.3
- ext-json: *
- illuminate/support: ^5.8|^6.0|^7.0|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- orchestra/testbench: ~3|~4|~5|~6
- phpunit/phpunit: ~7|~8|~9
- sempro/phpunit-pretty-print: ^1.0
This package is auto-updated.
Last update: 2024-10-25 15:09:13 UTC
README
This package helps too easily transform an array to a predefined class with setters and getters
For understanding better the use of this package please look at the Usage
as well as in the tests
folder where you can understand better how to use this class.
I created this package as it helps me a lot when I am working with JSON information in the DataBase that is structured into classes and subclasses.
Really hope that would be of some help to others as well!
Installation
You can install the package via composer:
composer require ag84ark/object-convertor
Usage
When creating classes for Laravel applications try to extend the BaseModelApi
as helps you with a lot of things.
Sample class
use ag84ark\ObjectConvertor\BaseModelApi; use ag84ark\ObjectConvertor\ObjectConstructorTrait; class SomeClass extends BaseModelApi { use ObjectConstructorTrait; private $a; private $b; private $other = ''; private $BigVar = ''; private $camelVar = ''; private $snake_var = ''; public function getBigVar(): string { return $this->BigVar; } public function setBigVar(string $BigVar): void { $this->BigVar = $BigVar; } /** * @return mixed */ public function getA() { return $this->a; } /** * @param mixed $a */ public function setA($a): void { $this->a = $a; } /** * @return mixed */ public function getB() { return $this->b; } /** * @param mixed $b */ public function setB($b): void { $this->b = $b; } public function getOther(): string { return $this->other; } public function setOther(string $other): void { $this->other = $other; } public function getCamelVar(): string { return $this->camelVar; } public function setCamelVar(string $camelVar): void { $this->camelVar = $camelVar; } public function getSnakeVar(): string { return $this->snake_var; } public function setSnakeVar(string $snake_var): void { $this->snake_var = $snake_var; } // .... }
Via constructor ( needs ObjectConstructorTrait )
$array = ['a' => 'val_a', 'b' => 'val_b', 'other' => 'val_other']; $someClassObject = new SomeClass($array);
From array ( needs ObjectConstructorTrait )
$array = ['a' => 'val_a', 'b' => 'val_b', 'other' => 'val_other']; $someClassObject = SomeClass::fromArray($array);
Via ObjectConvertor to class that extends BaseModelApi
$array = ['a' => 'val_a', 'b' => 'val_b', 'other' => 'val_other']; /** @var SomeClass $someClassObject */ $someClassObject = ObjectConvertor::toObjectBaseModelApi($array, new SomeClass());
Via ObjectConvertor to normal class
$array = ['a' => 'val_a', 'b' => 'val_b', 'other' => 'val_other']; /** @var SomeClass $someClassObject */ $someClassObject = ObjectConvertor::toObject($array, new SomeClass());
Basic class example
class BasicClass { private $var1; private $var2; /** * @return mixed */ public function getVar1() { return $this->var1; } /** * @param mixed $var1 */ public function setVar1($var1): void { $this->var1 = $var1; } /** * @return mixed */ public function getVar2() { return $this->var2; } /** * @param mixed $var2 */ public function setVar2($var2): void { $this->var2 = $var2; } } $array = ['var1' => 'val_a', 'var2' => 'val_b']; /** @var BasicClass $basicClassObject */ $basicClassObject = ObjectConvertor::toObject($array, new BasicClass());
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email arkangel062003@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.