PHPackages                             downsider/clay - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. downsider/clay

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

downsider/clay
==============

Create lightweight models and apply complex data structures to them

v3.1.2(7mo ago)26.9k2[1 PRs](https://github.com/lexide/clay/pulls)2MITPHPPHP &gt;=8.0CI passing

Since Jan 26Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/lexide/clay)[ Packagist](https://packagist.org/packages/downsider/clay)[ Docs](https://github.com/lexide/clay)[ RSS](/packages/downsider-clay/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (31)Used By (2)

Clay
====

[](#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.

installation
------------

[](#installation)

Install via composer

```
composer require lexide/clay

```

Simple Example
--------------

[](#simple-example)

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

```
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

```
$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:

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

```

Object instantiation
--------------------

[](#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

```
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
------------------

[](#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

```
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);

```

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance73

Regular maintenance activity

Popularity22

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity79

Established project with proven stability

 Bus Factor1

Top contributor holds 57.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~150 days

Recently: every ~532 days

Total

27

Last Release

219d ago

Major Versions

0.1.0 → 1.0.02015-03-24

1.3.0 → 2.0.02018-02-16

2.2.0 → 3.0.02021-09-09

PHP version history (3 changes)0.1.0PHP &gt;=5.3.0

1.0.0PHP &gt;=5.4.0

3.0.0PHP &gt;=8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a01f73f5eea8a755e31d879b50f5862ee8a20e8d73daba1c15cb7e75e68796db?d=identicon)[downsider](/maintainers/downsider)

---

Top Contributors

[![downsider](https://avatars.githubusercontent.com/u/4508388?v=4)](https://github.com/downsider "downsider (62 commits)")[![David-Denny](https://avatars.githubusercontent.com/u/35412695?v=4)](https://github.com/David-Denny "David-Denny (26 commits)")[![Danny-Smart](https://avatars.githubusercontent.com/u/113538899?v=4)](https://github.com/Danny-Smart "Danny-Smart (19 commits)")

---

Tags

modelmodelslightweight

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/downsider-clay/health.svg)

```
[![Health](https://phpackages.com/badges/downsider-clay/health.svg)](https://phpackages.com/packages/downsider-clay)
```

###  Alternatives

[wsdltophp/packagebase

Contains the base classes to be used by classes generated by wsdltophp/packagegenerator

234.6M93](/packages/wsdltophp-packagebase)[scrumble-nl/laravel-model-ts-type

This package makes it possible to generate TypeScript types based on your models

6984.5k](/packages/scrumble-nl-laravel-model-ts-type)[michalsn/codeigniter4-uuid

UUID and ULID package for CodeIgniter 4 with support for Model.

4728.7k4](/packages/michalsn-codeigniter4-uuid)[brysem/phpenums

Enums made simple in PHP.

10171.5k](/packages/brysem-phpenums)[mpociot/reanimate

Undo Laravel soft deletes

1221.2k](/packages/mpociot-reanimate)[adamhopkinson/laravel-model-hash

A trait which automatically generates a unique hash per model instance

2318.7k](/packages/adamhopkinson-laravel-model-hash)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
