Skip to content

Latest commit

 

History

History
70 lines (58 loc) · 2.28 KB

readme.md

File metadata and controls

70 lines (58 loc) · 2.28 KB

PHP Data Object

Total Downloads Latest Stable Version License wakatime Tests


PHP Light DataObject with minimum implementation.

Installation

From the command line run:

composer require saedyousef/dataobject

Usage

With the package now installed, you can implement the main Interface DataObject and by using the trait DataObjectTrait that have all the methods implemented for you. Here is an example of a class PostDataObject that implements the DataObject interface :

use SY\DataObject\Contracts\DataObject;
use SY\DataObject\Support\DataObjectTrait;
use SY\DataObject\Support\ObjectReadAccess;
use SY\DataObject\Support\ObjectWriteAccess;

/**
 * @property int|null id
 * @property string title
 * @property string body 
 */
class PostDataObject implements DataObject
{
    use DataObjectTrait;
    use ObjectReadAccess;
    use ObjectWriteAccess; // If you need to write object properties.
    
    public function __construct(array $properties = [])
    {
        $this->_properties = [
            'id'    => null,
            'title' => '',
            'body'  => ''
        ];

        $this->hydrate($properties);
    }
    
    /** 
    * @return int|null
    */
    public function getId()
    {
        return $this->id;
    }
    
    /**
    * @return string
    */
    public function getTitle(): string
    {
        return $this->title;
    }
}