akbarali/data-object

Data Object PHP Laravel

2.8.0 2024-12-06 07:14 UTC

This package is auto-updated.

Last update: 2024-12-06 07:15:13 UTC


README

composer require akbarali/data-object

TODO

USAGE

Array create Data Object

$object = DataObj::createFromArray([
    'key1' => 'value1',
    'key2' => 'value2',
]);

Laravel model create Data Object

$model = User::query()->find(1);
$object = DataObj::createFromEloquentModel($model);

Json create Data Object

$object = DataObj::createFromJson('{"key1":"value1","key2":"value2"}');

Laravel Relation BelongsTo or HasOne

Create StoreData

class StoreData extends \Akbarali\DataObject\DataObjectBase
{
    public ?string $id;
    public ?int    $user_id;
    public ?string $name;
    public ?string $phone;
    public ?string $address;
    public ?string $description = "Default Description";
    public ?string $image;
    public ?bool   $status;
    public ?string $created_at;

    public UserData $user;
}

UserData

class UserData extends \Akbarali\DataObject\DataObjectBase
{
    public ?int    $id;
    public ?string $name;
    public ?string $email;
    public ?string $avatar;
    public ?string $phone;
    public ?string $birth_date;
    public ?bool   $status;
    public ?string $created_at;

    public RoleData $role;
}

RoleData

class RoleData extends \Akbarali\DataObject\DataObjectBase
{
    public int    $id;
    public string $name;
    public string $slug;
    public string $created_at;
}
$store = Store::query()->with(['user.role'])->find(1);
$storeData = StoreData::createFromEloquentModel($store);

Note: If you want to turn the relation into a DataObject, you should open a realton to the model and get that relation with. Then U is also converted to data object

Laravel Relation HasMany

ProductData

ProductData extends \Akbarali\DataObject\DataObjectBase
{
    public ?int    $id;
    public ?string $name;
    public ?string $description;
    public ?string $image;
    public ?bool   $status;
    public ?string $created_at;
}

StoreData

StoreData extends \Akbarali\DataObject\DataObjectBase
{
    public ?int    $id;
    public ?string $name;
    public ?string $description;
    public ?string $image;
    public ?bool   $status;
    public ?string $created_at;
    
    public array|ProductData $products;
}
$store = Store::query()->with(['products'])->find(1);
$storeData = StoreData::createFromEloquentModel($store);
//or
$storeData = StoreData::fromModel($store);

DataObject To array

$storeData->toArray();
//or
$storeData->all(true);

DataObjec To SnakCase

$storeData->toSnakeCase();

In a model, created_at is usually use Illuminate\Support\Carbon;. You can also pass created_at to Carbon.

use Illuminate\Support\Carbon;

ProductData extends \Akbarali\DataObject\DataObjectBase
{
    public int    $id;
    public string $name;
    public string $description;
    public string $image;
    public bool   $status;
    public Carbon $created_at;
}

2.0 version

Supported Readonly Properties

class ClientData extends \Akbarali\DataObject\DataObjectBase
{
    public readonly int $id;
    public string       $full_name;
}
$object = DataObj::createFromArray([
    'id'        => 1,
    'full_name' => 'Akbarali',
]);
$object->id = 2;

Error: Cannot modify readonly property App\DataObjects\HistoryData::$id

2.2 version

Adding: fromModel fromJson fromArray

Add static function arrayToClassProperty

class ClientData extends \Akbarali\DataObject\DataObjectBase
{
    public int    $id;
    public string $full_name;
}
$object = ClientData::arrayToClassProperty([
    'id'        => 1,
    'full_name' => 'Akbarali',
]); 

Return string: public readonly int $id;public string $full_name;

2.5 version

createProperty method added to create a property return string and bug fix arrayToClassProperty

2.6.2 version

Add command create DataObject

Install

Add DataObjectProvider to /config/app.php providers

'providers' => [
...
\Akbarali\DataObject\DataObjectProvider::class,
]

DataObject create console command

 php artisan do:create

Models Create DataObject

 php artisan do:create models

Tables Create DataObject

 php artisan do:create tables

Search Table and create DataObject

 php artisan do:create tableSearch

Search Model and create DataObject

 php artisan do:create modelSearch

2.6.5 version

Add --model and --table options to create DataObject

php artisan do:create --model="App\Models\QQB\User"
php artisan do:create --table="users"