Skip to content

PHP Data Object

License

Notifications You must be signed in to change notification settings

saedyousef/dataobject

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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;
    }
}

Support Me!

Buy Me a Coffee at ko-fi.com