{"id":20140245,"url":"https://github.com/10quality/php-data-model","last_synced_at":"2025-06-17T07:33:05.453Z","repository":{"id":56937900,"uuid":"135376385","full_name":"10quality/php-data-model","owner":"10quality","description":"Library that provides generic and scalable Data Models for any type of data handling.","archived":false,"fork":false,"pushed_at":"2023-01-05T16:45:20.000Z","size":13,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"v1.0","last_synced_at":"2025-02-16T02:48:05.294Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/10quality.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-30T02:21:08.000Z","updated_at":"2023-01-05T16:44:14.000Z","dependencies_parsed_at":"2023-02-04T07:32:20.557Z","dependency_job_id":null,"html_url":"https://github.com/10quality/php-data-model","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fphp-data-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fphp-data-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fphp-data-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/10quality%2Fphp-data-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/10quality","download_url":"https://codeload.github.com/10quality/php-data-model/tar.gz/refs/heads/v1.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241583984,"owners_count":19986078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T21:49:52.412Z","updated_at":"2025-03-02T23:22:52.987Z","avatar_url":"https://github.com/10quality.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data Model (PHP)\n\n[![Latest Stable Version](https://poser.pugx.org/10quality/php-data-model/v/stable)](https://packagist.org/packages/10quality/php-data-model)\n[![Total Downloads](https://poser.pugx.org/10quality/php-data-model/downloads)](https://packagist.org/packages/10quality/php-data-model)\n[![License](https://poser.pugx.org/10quality/php-data-model/license)](https://packagist.org/packages/10quality/php-data-model)\n\nPHP Library that provides generic and scalable Data Models (abstract model class) for any type of data handling.\n\nThis perfect and suitable for when developing MVC frameworks, Api clients, wrappets and/or any type of project that requires data handling.\n\n*Models inspired by [Laravel](https://laravel.com/) and our very own [Wordpress MVC](https://www.wordpress-mvc.com/).*\n\n## Requires\n\n* PHP \u003e= 5.4\n\n## Usage\n\nWhen defining your models, extend them from the `Model` abstract class.\n\nIn the following example, a `Product` data model will be created and will extend from the `Model` abstract class.\n```php\n\nuse TenQuality\\Data\\Model;\n\nclass Product extends Model\n{\n}\n\n```\n\nThen instantiate your models like this:\n```php\n$product = new Product;\n```\n\n### Adding data\n\nVery easy, simply add the data like an object property.\n```php\n// Set data\n$product-\u003eprice = 19.99;\n$product-\u003ename = 'My product';\n$product-\u003ebrandName = '10 Quality';\n\n// Get data\necho $product-\u003eprice; // Will echo 19.99\necho $product-\u003ename; // Will echo My product\necho $product-\u003ebrandName; // Will echo 10 Quality\n```\n\n### Creating aliases\n\nAliases are model properties that are set or get by class methods. This are defined in the model.\n\nIn the following example, an alias will be created in the model to display prices with currenty.\n\n```php\n\nuse TenQuality\\Data\\Model;\n\nclass Product extends Model\n{\n    /**\n     * Method for alias property `displayPrice`.\n     * Return `price` property concatenated with the $ (dollar) symbol\n     */\n    protected function getDisplayPriceAlias()\n    {\n        return '$'.$this-\u003eprice;\n    }\n\n    /**\n     * Method for alias property `displayPrice`.\n     * Sets a the price value if the alias is used.\n     */\n    protected function setDisplayPriceAlias($value)\n    {\n        $this-\u003eprice = floatval(str_replace('$', '', $value));\n    }\n}\n\n```\n\nThen use it like this:\n```php\n$product-\u003eprice = 19.99;\n\n// Get alias property\necho $product-\u003edisplayPrice; // Will echo $19.99\n\n// Set alias property\n$product-\u003edisplayPrice = 29.99;\n\n// Echo property\necho $product-\u003eprice; // Will echo 29.99\necho $product-\u003edisplayPrice; // Will echo $29.99\n```\n\nYou can also init the models with the construct method (passing properties as an array):\n```php\n$product = new Product(['price' =\u003e 19.99]);\n\n// Echo property\necho $product-\u003eprice; // Will echo 19.99\n```\n\n### Casting\n\nBefore 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:\n\n```php\n\nuse TenQuality\\Data\\Model;\n\nclass Product extends Model\n{\n    protected $properties = [\n        'name',\n        'price',\n        'displayPrice',\n    ];\n}\n```\n\nNotice that both properties and aliases can be listed. Following the samples above, property `brandName` will stay hidden from casting.\n\nCast the model like this:\n```php\nvar_dump($model-\u003etoArray()); // To array\nvar_dump($model-\u003e__toArray()); // To array\n\necho (string)$model; // To json encoded string\n\necho $model-\u003etoJSON(); // To json encoded string\necho $model-\u003e__toJSON(); // To json encoded string\n\n// You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation\n// http://php.net/manual/en/function.json-encode.php\necho $model-\u003etoJSON(JSON_NUMERIC_CHECK, 600);\n```\n\n### Collections\n\nThis package also provides a `Collection` class that will facilitate the use of multiple models or data records.\n\nCollections behave like normal arrays would:\n```php\nuse TenQuality\\Data\\Collection;\n\n$collection = new Collection;\n// Add your models as you would normally do with arrays\n$collection[] = $product;\n$collection[] = $product;\n\necho count($collection); // Thrown count\nvar_dump($collection[0]); // Dumps first model added\n\n// Loop a collection\nforeach ($collection as $product) {\n    // Do your stuff\n}\n```\n\nCollections can be sorted very easily:\n```php\nuse TenQuality\\Data\\Collection;\n\n$collection = new Collection;\n// Add your models as you would normally do with arrays\n$collection[] = new Product(['price' =\u003e 99.99]);\n$collection[] = new Product(['price' =\u003e 2.99]);\n\n// Loop a collection\nforeach ($collection-\u003esortBy('price') as $product) {\n    echo $product-\u003eprice; // 2.99 will display first and then 99.99\n}\n\n// Change sorting criteria\n// http://php.net/manual/en/array.constants.php\nprint_r($collection-\u003esortBy('price', SORT_NUMERIC));\n```\n\nData in a collection can be grouped very easily:\n```php\nuse TenQuality\\Data\\Collection;\n\n$collection = new Collection;\n// Add your models as you would normally do with arrays\n$collection[] = new Fruit(['name' =\u003e 'Apple', 'color' =\u003e 'red']);\n$collection[] = new Fruit(['name' =\u003e 'Banana', 'color' =\u003e 'yellow']);\n$collection[] = new Fruit(['name' =\u003e 'Strawberry', 'color' =\u003e 'red']);\n$collection[] = new Fruit(['name' =\u003e 'Orange', 'color' =\u003e 'orange']);\n\n// Loop a collection\nforeach ($collection-\u003egroupBy('color') as $color =\u003e $fruits) {\n    // This will group the data in sub arrays based on colors\n    echo $color;\n    foreach ($fruits as $fruit) {\n        echo $fruit; // Fruit in the color grouped by.\n    }\n}\n```\n\nCast the colleciton like this:\n```php\nvar_dump($collection-\u003etoArray()); // To array\nvar_dump($collection-\u003e__toArray()); // To array\n\necho (string)$collection; // To json encoded string\n\necho $collection-\u003etoJSON(); // To json encoded string\necho $collection-\u003e__toJSON(); // To json encoded string\n\n// You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation\n// http://php.net/manual/en/function.json-encode.php\necho $collection-\u003etoJSON(JSON_NUMERIC_CHECK, 600);\n```\n\n## Guidelines\n\nPSR-2 coding standards.\n\n## Copyright and License\n\nMIT License - (C) 2018 [10 Quality](https://www.10quality.com/).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F10quality%2Fphp-data-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F10quality%2Fphp-data-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F10quality%2Fphp-data-model/lists"}