PHPackages                             10quality/php-data-model - 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. 10quality/php-data-model

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

10quality/php-data-model
========================

PHP Library that provides generic and scalable Data Models (abstract model class) for any type of data handling.

v1.0.3(3y ago)123.4k↓32.4%11MITPHPPHP &gt;=5.4

Since May 30Pushed 3y ago2 watchersCompare

[ Source](https://github.com/10quality/php-data-model)[ Packagist](https://packagist.org/packages/10quality/php-data-model)[ Docs](https://github.com/10quality/php-data-model)[ RSS](/packages/10quality-php-data-model/feed)WikiDiscussions v1.0 Synced 2d ago

READMEChangelog (4)Dependencies (1)Versions (6)Used By (1)

Data Model (PHP)
================

[](#data-model-php)

[![Latest Stable Version](https://camo.githubusercontent.com/0ac688fd7fa4eb3bf8be602b62ba0507395a450a20e5a9fa6476980cdef022c8/68747470733a2f2f706f7365722e707567782e6f72672f31307175616c6974792f7068702d646174612d6d6f64656c2f762f737461626c65)](https://packagist.org/packages/10quality/php-data-model)[![Total Downloads](https://camo.githubusercontent.com/085149c0e4200bef56982822188ffe077223ae9db7671a2d902cd4e1a07d9fc4/68747470733a2f2f706f7365722e707567782e6f72672f31307175616c6974792f7068702d646174612d6d6f64656c2f646f776e6c6f616473)](https://packagist.org/packages/10quality/php-data-model)[![License](https://camo.githubusercontent.com/4c1acfb53137760d178d4550538b33e42d9ab79766d814527aca6b9b06941175/68747470733a2f2f706f7365722e707567782e6f72672f31307175616c6974792f7068702d646174612d6d6f64656c2f6c6963656e7365)](https://packagist.org/packages/10quality/php-data-model)

PHP Library that provides generic and scalable Data Models (abstract model class) for any type of data handling.

This perfect and suitable for when developing MVC frameworks, Api clients, wrappets and/or any type of project that requires data handling.

*Models inspired by [Laravel](https://laravel.com/) and our very own [Wordpress MVC](https://www.wordpress-mvc.com/).*

Requires
--------

[](#requires)

- PHP &gt;= 5.4

Usage
-----

[](#usage)

When defining your models, extend them from the `Model` abstract class.

In the following example, a `Product` data model will be created and will extend from the `Model` abstract class.

```
use TenQuality\Data\Model;

class Product extends Model
{
}
```

Then instantiate your models like this:

```
$product = new Product;
```

### Adding data

[](#adding-data)

Very easy, simply add the data like an object property.

```
// Set data
$product->price = 19.99;
$product->name = 'My product';
$product->brandName = '10 Quality';

// Get data
echo $product->price; // Will echo 19.99
echo $product->name; // Will echo My product
echo $product->brandName; // Will echo 10 Quality
```

### Creating aliases

[](#creating-aliases)

Aliases are model properties that are set or get by class methods. This are defined in the model.

In the following example, an alias will be created in the model to display prices with currenty.

```
use TenQuality\Data\Model;

class Product extends Model
{
    /**
     * Method for alias property `displayPrice`.
     * Return `price` property concatenated with the $ (dollar) symbol
     */
    protected function getDisplayPriceAlias()
    {
        return '$'.$this->price;
    }

    /**
     * Method for alias property `displayPrice`.
     * Sets a the price value if the alias is used.
     */
    protected function setDisplayPriceAlias($value)
    {
        $this->price = floatval(str_replace('$', '', $value));
    }
}
```

Then use it like this:

```
$product->price = 19.99;

// Get alias property
echo $product->displayPrice; // Will echo $19.99

// Set alias property
$product->displayPrice = 29.99;

// Echo property
echo $product->price; // Will echo 29.99
echo $product->displayPrice; // Will echo $29.99
```

You can also init the models with the construct method (passing properties as an array):

```
$product = new Product(['price' => 19.99]);

// Echo property
echo $product->price; // Will echo 19.99
```

### Casting

[](#casting)

Before using any casting options, the model needs to define which properties are visible and which are hidden. This is done by listing the visible ones like this:

```
use TenQuality\Data\Model;

class Product extends Model
{
    protected $properties = [
        'name',
        'price',
        'displayPrice',
    ];
}
```

Notice that both properties and aliases can be listed. Following the samples above, property `brandName` will stay hidden from casting.

Cast the model like this:

```
var_dump($model->toArray()); // To array
var_dump($model->__toArray()); // To array

echo (string)$model; // To json encoded string

echo $model->toJSON(); // To json encoded string
echo $model->__toJSON(); // To json encoded string

// You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation
// http://php.net/manual/en/function.json-encode.php
echo $model->toJSON(JSON_NUMERIC_CHECK, 600);
```

### Collections

[](#collections)

This package also provides a `Collection` class that will facilitate the use of multiple models or data records.

Collections behave like normal arrays would:

```
use TenQuality\Data\Collection;

$collection = new Collection;
// Add your models as you would normally do with arrays
$collection[] = $product;
$collection[] = $product;

echo count($collection); // Thrown count
var_dump($collection[0]); // Dumps first model added

// Loop a collection
foreach ($collection as $product) {
    // Do your stuff
}
```

Collections can be sorted very easily:

```
use TenQuality\Data\Collection;

$collection = new Collection;
// Add your models as you would normally do with arrays
$collection[] = new Product(['price' => 99.99]);
$collection[] = new Product(['price' => 2.99]);

// Loop a collection
foreach ($collection->sortBy('price') as $product) {
    echo $product->price; // 2.99 will display first and then 99.99
}

// Change sorting criteria
// http://php.net/manual/en/array.constants.php
print_r($collection->sortBy('price', SORT_NUMERIC));
```

Data in a collection can be grouped very easily:

```
use TenQuality\Data\Collection;

$collection = new Collection;
// Add your models as you would normally do with arrays
$collection[] = new Fruit(['name' => 'Apple', 'color' => 'red']);
$collection[] = new Fruit(['name' => 'Banana', 'color' => 'yellow']);
$collection[] = new Fruit(['name' => 'Strawberry', 'color' => 'red']);
$collection[] = new Fruit(['name' => 'Orange', 'color' => 'orange']);

// Loop a collection
foreach ($collection->groupBy('color') as $color => $fruits) {
    // This will group the data in sub arrays based on colors
    echo $color;
    foreach ($fruits as $fruit) {
        echo $fruit; // Fruit in the color grouped by.
    }
}
```

Cast the colleciton like this:

```
var_dump($collection->toArray()); // To array
var_dump($collection->__toArray()); // To array

echo (string)$collection; // To json encoded string

echo $collection->toJSON(); // To json encoded string
echo $collection->__toJSON(); // To json encoded string

// You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation
// http://php.net/manual/en/function.json-encode.php
echo $collection->toJSON(JSON_NUMERIC_CHECK, 600);
```

Guidelines
----------

[](#guidelines)

PSR-2 coding standards.

Copyright and License
---------------------

[](#copyright-and-license)

MIT License - (C) 2018 [10 Quality](https://www.10quality.com/).

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~420 days

Total

5

Last Release

1275d ago

### Community

Maintainers

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

---

Top Contributors

[![amostajo](https://avatars.githubusercontent.com/u/1645908?v=4)](https://github.com/amostajo "amostajo (5 commits)")

---

Tags

datamodels

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/10quality-php-data-model/health.svg)

```
[![Health](https://phpackages.com/badges/10quality-php-data-model/health.svg)](https://phpackages.com/packages/10quality-php-data-model)
```

###  Alternatives

[fakerphp/faker

Faker is a PHP library that generates fake data for you.

4.0k390.0M4.6k](/packages/fakerphp-faker)[dflydev/dot-access-data

Given a deep data structure, access data by dot notation.

722393.7M110](/packages/dflydev-dot-access-data)[mbezhanov/faker-provider-collection

A collection of custom providers for the Faker library

2179.4M25](/packages/mbezhanov-faker-provider-collection)[php-units-of-measure/php-units-of-measure

A PHP library for converting between standard units of measure.

3143.8M36](/packages/php-units-of-measure-php-units-of-measure)[amenadiel/jpgraph

Composer Friendly, full refactor of JpGraph, library to make graphs and charts

1532.4M7](/packages/amenadiel-jpgraph)[jbzoo/data

An extended version of the ArrayObject object for working with system settings or just for working with data arrays

851.6M23](/packages/jbzoo-data)

PHPackages © 2026

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