PHP code example of czim / laravel-dataobject

1. Go to this page and download the library: Download czim/laravel-dataobject library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

czim / laravel-dataobject example snippets


class TestDataObject extends \Czim\DataObject\AbstractDataObject
{
    public function getName($value)
    {
        $this->getAttribute('name');
    }

    public function setName($value)
    {
        $this->setAttribute('name', $value);
    }
}

class TestDataObject extends \Czim\DataObject\AbstractDataObject
{
    protected $magicAssignment = false;

    ...

class YourDataObject extends \Czim\DataObject\AbstractDataObject
{
    protected $rules = [
        'name' => '

    $dataObject = new YourDataObject();

    // validate() returns a boolean, false if it does not follow the rules
    if ( ! $dataObject->validate()) {

        $messages = $dataObject->messages();

        // messages() returns a standard Laravel MessageBag
        dd( $messages->first() );
    }

    Czim\DataObject\DataObjectServiceProvider::class,


protected function casts()
{
    return [
        'check' => 'boolean',
        'count' => 'integer',
        'price' => 'float',
    ];
}


$object = new YourDataObject([
    'check' => 'truthy value',
    'price' => 45,
]);

$object->check;     // true
$object['price'];   // 45.0
$object->count;     // 0 (instead of null)


class RootDataObject extends \Czim\DataObject\CastableDataObject
{
    protected function casts()
    {
        return [
            'some_object' => YourDataObject::class,
            'object_list' => YourDataObject::class . '[]',
        ];
    }
}


$data = [
    'some_object' => [
        'type' => 'peaches',
    ],
    'object_list' => [
        ['type' => 'cucumbers'],
        ['type' => 'sherry'],
    ],
];

$object = new RootDataobject($data);

$object->some_object;           // instance of YourDataObject
$object->some_object->type;     // peaches
$object->object_list[1]->type;  // sherry