2017 © Pedro Peláez
 

library clay

Create lightweight models and apply complex data structures to them

image

downsider/clay

Create lightweight models and apply complex data structures to them

  • Friday, March 2, 2018
  • by downsider
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5,329 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 24 Versions
  • 5 % Grown

The README.md

Clay

Clay allows you to populate a model object with an array of data (from a database or other data store) without having to manually apply the values by hand. Data is applied to the model based on the name of each property, either directly or via setters if they are available. Clay supports nested array data and properties which contain other objects (in a multi layered data structure) as well as automatically converting keys to camel case when applying data to the model., (*1)

installation

Install via composer, (*2)

composer require lexide/clay

Simple Example

To use Clay, a model should use the ModelTrait trait and implement a method which calls loadData(), (*3)

class Model
{
    use Lexide\Clay\Model\ModelTrait;

    public $property;
    ...

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }

}

The model can then be instantiated, passing data to the constructor, (*4)

$data = ["property" => "value", ...];
$model = new Model($data);

echo $model->property; // outputs "value"

Data with underscored or spaced keys will be applied to their camel case counterparts:, (*5)

$data = [
    "long_field_name" => ... // will apply to the property "longFieldName"
    "property name with spaces" => ... // will apply to "propertyNameWithspaces"
];

Object instantiation

If you want a subset of the data to be contained within a second class, you simply need to type-hint the supplied argument in a setter, (*6)

class Address
{
    use Lexide\Clay\Model\ModelTrait;

    public $street;
    public $city;

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }
}

class Customer
{
    use Lexide\Clay\Model\ModelTrait;

    protected $address;

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }

    public function setAddress(Address $address)
    {
        $this->address = $address;
    }

}

$data = [
    "address": [
        "street" => "1 test street",
        "city" => "exampleton"
    ]
]

$customer = new Customer($data);
echo $customer->getAddress()->city; // outputs "exampleton"

Object collections

The same can be done for an array or collection of objects. Clay looks for an "add" method for that property to determine the class to instantiate, (*7)

class Customer
{
    use Lexide\Clay\Model\ModelTrait;

    protected $addresses = [];

    public function __construct(array $data = [])
    {
        $this->loadData($data);
    }

    public function setAddresses(array $addresses)
    {
        $this->addresses = [];
        foreach ($addresses as $address) {
            $this->addAddresses($address));
        }
    }

    public function addAddresses(Address $address)
    {
        $this->addresses[] = $address;
    }

}

$data = [
    "addresses": [
        [
            "street" => "1 Test street",
            "city" => "Exampleton"
        ],
        [
            "street" => "22 Sample row",
            "city" => "Testville"
        ]
    ]
]

$customer = new Customer($data);

The Versions

02/03 2018

dev-master

9999999-dev https://github.com/lexide/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

 

The Development Requires

by Danny Smart

lightweight model models

02/03 2018

dev-develop

dev-develop https://github.com/lexide/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

 

The Development Requires

by Danny Smart

lightweight model models

02/03 2018

2.1.0

2.1.0.0 https://github.com/lexide/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

 

The Development Requires

by Danny Smart

lightweight model models

21/02 2018

2.0.1

2.0.1.0 https://github.com/lexide/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Danny Smart

lightweight model models

16/02 2018

2.0.0

2.0.0.0 https://github.com/lexide/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Danny Smart

lightweight model models

21/03 2017

1.3.0

1.3.0.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

28/02 2017

1.2.11

1.2.11.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

09/02 2017

1.2.10

1.2.10.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

08/02 2017

1.2.9

1.2.9.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

18/04 2016

1.2.8

1.2.8.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

27/03 2016

1.2.7

1.2.7.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

16/12 2015

1.2.6

1.2.6.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

15/12 2015

1.2.5

1.2.5.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

09/12 2015

1.2.4

1.2.4.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

08/12 2015

1.2.3

1.2.3.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

07/12 2015

1.2.2

1.2.2.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

07/12 2015

1.2.1

1.2.1.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

18/11 2015

1.2.0

1.2.0.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

28/10 2015

1.1.2

1.1.2.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

28/10 2015

1.1.1

1.1.1.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

28/10 2015

1.1.0

1.1.0.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

24/03 2015

1.0.1

1.0.1.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

24/03 2015

1.0.0

1.0.0.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

lightweight model models

26/01 2015

0.1.0

0.1.0.0 https://github.com/downsider/clay

Create lightweight models and apply complex data structures to them

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

lightweight model models