2017 © Pedro Peláez
 

library php_data_object

Simplified Data Container (inspired by Varien_Object).

image

flancer32/php_data_object

Simplified Data Container (inspired by Varien_Object).

  • Sunday, January 21, 2018
  • by flancer32
  • Repository
  • 2 Watchers
  • 1 Stars
  • 4,724 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 7 Versions
  • 7 % Grown

The README.md

php_data_object

Build Status codecov.io, (*1)

_"Smart data structures and dumb code works a lot better than the other way around."_ (c) Eric S. Raymond, (*2)

_"Bad programmers worry about the code. Good programmers worry about data structures and their relationships."_ (c) Linus Torvalds, (*3)

Overview

This is yet another PHP implementation of the data container (like DTO / SDO). Some kind of the wrapper around associative array. The main goal of this implementation is to be an accessor for the raw data., (*4)

Native PHP objects

Structure

We can use any property of any PHP object:, (*5)

$obj1 = new class {};
$obj2 = new class {};
$obj1->name = 'first';
$obj2->code = 'OBJ2';
$obj1->sub = $obj2;
$this->assertEquals('first', $obj1->name);
$this->assertEquals('OBJ2', $obj1->sub->code);

More..., (*6)

Paths

We can set/get value of the inner property in PHP style:, (*7)

$obj->sub->code = $code;
$code = $obj->sub->code;

but we will have "Undefined property" error if $obj->sub property does not exist., (*8)

Type checking

We need to use accessors to control properties types., (*9)

This is Customer class with string property:, (*10)

/**
 * @property string $name Customer name.
 */
class Customer
{
    public function getName() : string
    {
        return $this->name;
    }

    public function setName(string $data)
    {
        $this->name = $data;
    }
}

This is Order class with Customer property:, (*11)

/**
 * @property Customer $customer
 */
class Order
{
    public function getCustomer() : Customer
    {
        return $this->customer;
    }

    public function setCustomer(Customer $data)
    {
        $this->customer = $data;
    }
}

This is code without errors (all types are expected):, (*12)

$customer = new Customer();
$customer->setName('John Dow');
$order = new Order();
$order->setCustomer($customer);
$this->assertTrue(is_string($order->getCustomer()->getName()));

This code will throw a \TypeError exception:, (*13)

$customer = new class {};
$customer->name = 'John Dow';
$order = new Order();
$order->setCustomer($customer);

More..., (*14)

Data Objects

Structure

Paths

With paths we will have property value if chain of properties exists or null otherwise:, (*15)

$code = $obj->get('sub/code');
$code = $obj->get('/sub/code');    // equals to 'sub/code'
$code = $obj->get('/subs/0/code'); // 'subs' is array
$code = $obj->get('/sub/code/does/not/exist'); // 'null' is returned, no error is occured

Also we can set data property by path:, (*16)

$obj->set('order/customer/name', 'John Dow');

Type hinting

Installation

Add to composer.json:, (*17)

"require": {
    "flancer32/php_data_object": "0.1.0"
}

Development

$ composer install
$ ./vendor/bin/phpunit -c ./test/unit/phpunit.dist.xml

The Versions

21/01 2018

dev-master

9999999-dev https://github.com/flancer32/php_data_object

Simplified Data Container (inspired by Varien_Object).

  Sources   Download

MIT

The Development Requires

by Alex Gusev

10/07 2017

0.2.1

0.2.1.0 https://github.com/flancer32/php_data_object

Universal Data Container (based on Varien_Object).

  Sources   Download

MIT

The Development Requires

by Alex Gusev

17/03 2017

0.2.0

0.2.0.0 https://github.com/flancer32/php_data_object

Universal Data Container (based on Varien_Object).

  Sources   Download

MIT

The Development Requires

by Alex Gusev

20/04 2016

0.1.3

0.1.3.0 https://github.com/flancer32/php_data_object

Universal Data Container (based on Varien_Object).

  Sources   Download

MIT

The Development Requires

by Alex Gusev

15/04 2016

0.1.2

0.1.2.0 https://github.com/flancer32/php_data_object

Universal Data Container (based on Varien_Object).

  Sources   Download

MIT

The Development Requires

by Alex Gusev

10/03 2016

0.1.1

0.1.1.0 https://github.com/flancer32/php_data_object

Universal Data Container (based on Varien_Object).

  Sources   Download

MIT

The Development Requires

by Alex Gusev

15/02 2016

0.1.0

0.1.0.0 https://github.com/flancer32/php_data_object

Universal Data Container (based on Varien_Object).

  Sources   Download

MIT

The Development Requires

by Alex Gusev