ag84ark/object-convertor

Array to object convert

1.2.0 2022-08-25 10:15 UTC

This package is auto-updated.

Last update: 2024-10-25 15:09:13 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

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.