endorphin-studio/data-object

Simple Library to Work with base object

1.0.2 2021-08-19 16:05 UTC

This package is auto-updated.

Last update: 2024-12-19 23:50:30 UTC


README

Create object from array and work with it with helper functions getFieldName(), setFieldName($value)

Code Status

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality

About

Author: Serhii Nekhaienko
Email: serhii.nekhaienko@gmail.com
Stable Version: 1.0.0
License: MIT

Requirements

PHP >=7.4
JSON extension

Install via Composer

composer require endorphin-studio/data-object

Basic Usage

use EndorphinStudio\DataObject\DataObject;

class Role extends DataObject {

}

class User extends DataObject {
    protected array $fieldTypeMapping = [
        'roles' => Role::class
    ];
    
    protected array $listFields = [
        'roles'
    ];
}

$userData = [
    'name' => 'Serhii',
    'login' => 'serhii',
    'roles' => [
        [
            'name' => 'admin'
        ],
        [
            'name' => 'editor'
        ]
    ]
];

$user = new User($userData);

echo $user->getName(); // Serhii
echo $user->getLogin(); // serhii

foreach($user->getRoles() as $role) {
    echo $role->getName(); // admin, editor
}