{"id":22125711,"url":"https://github.com/czim/laravel-dataobject","last_synced_at":"2025-07-25T16:32:42.702Z","repository":{"id":56960944,"uuid":"43203909","full_name":"czim/laravel-dataobject","owner":"czim","description":"Basic framework for making standardized but flexible data objects for Laravel.","archived":false,"fork":false,"pushed_at":"2023-04-10T09:19:12.000Z","size":93,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-08T10:21:13.655Z","etag":null,"topics":["dataobject","laravel","validation"],"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/czim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-26T12:05:04.000Z","updated_at":"2023-03-21T18:23:54.000Z","dependencies_parsed_at":"2024-06-21T07:28:20.932Z","dependency_job_id":"796fa59e-deec-4156-b2a0-0119e22783b1","html_url":"https://github.com/czim/laravel-dataobject","commit_stats":{"total_commits":86,"total_committers":4,"mean_commits":21.5,"dds":"0.39534883720930236","last_synced_commit":"9b08e5ef93ed5a48091ed20403824b4eb2178eff"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-dataobject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-dataobject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-dataobject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czim%2Flaravel-dataobject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czim","download_url":"https://codeload.github.com/czim/laravel-dataobject/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227594876,"owners_count":17791203,"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":["dataobject","laravel","validation"],"created_at":"2024-12-01T16:37:16.866Z","updated_at":"2024-12-01T16:37:17.376Z","avatar_url":"https://github.com/czim.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Data Object\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE.md)\n[![Build Status](https://travis-ci.org/czim/laravel-dataobject.svg?branch=master)](https://travis-ci.org/czim/laravel-dataobject)\n[![Coverage Status](https://coveralls.io/repos/github/czim/laravel-dataobject/badge.svg?branch=master)](https://coveralls.io/github/czim/laravel-dataobject?branch=master)\n[![Latest Stable Version](http://img.shields.io/packagist/v/czim/laravel-dataobject.svg)](https://packagist.org/packages/czim/laravel-dataobject)\n[![SensioLabsInsight](https://insight.sensiolabs.com/projects/369fd4d7-b2d1-4438-9e08-e7ad586b81c4/mini.png)](https://insight.sensiolabs.com/projects/369fd4d7-b2d1-4438-9e08-e7ad586b81c4)\n\nBasic framework for making standardized but flexible data objects.\nThis provides a class (and interfaces) to use to standardize your dependencies or return values with a generic dataobject.\n\nAll this really is, is a data storage class with magic getters and setters.\nIt is Arrayable, Jsonable and validatable.\n\nAlso provided are the means to make nested validation possible for data objects (containing futher data objects).\n\n\n## Version Compatibility\n\n| Laravel    | Package  | PHP                     |\n|:-----------|:---------|:------------------------|\n| 5.1        | 1.0      |                         |\n| 5.2        | 1.0      |                         |\n| 5.3        | 1.3      |                         |\n| 5.4        | 1.4      |                         |\n| 5.5 and up | 1.4, 2.0 |                         |\n| 9 and up   | 3.0      | 8.0 and up              |\n\n## Install\n\nVia Composer\n\n``` bash\n$ composer require czim/laravel-dataobject\n```\n\n## Usage\n\nSimply create your own extension of the base dataobject, and:\n\n\n### Optionally add your own getters and setters\n\nBasic stuff of course, but if you want your IDE to know what your objects contain, you can simply write getters and setters like this:\n\n```php\nclass TestDataObject extends \\Czim\\DataObject\\AbstractDataObject\n{\n    public function getName($value)\n    {\n        $this-\u003egetAttribute('name');\n    }\n\n    public function setName($value)\n    {\n        $this-\u003esetAttribute('name', $value);\n    }\n}\n```\n\nAdditionally, if you want to block any type of magic assignment (meaning all assignment would have to be done through the `setAttribute()` or `setAttributes()` methods, or your own setters), you can disable magic assignment as follows:\n\n```php\nclass TestDataObject extends \\Czim\\DataObject\\AbstractDataObject\n{\n    protected $magicAssignment = false;\n\n    ...\n```\n\nAttempts to set attributes on the DataObject by magic or array access will then throw an `UnassignableAttributeException`.\n\n\n### Optionally add validation rules for attributes\n\n```php\nclass YourDataObject extends \\Czim\\DataObject\\AbstractDataObject\n{\n    protected $rules = [\n        'name' =\u003e 'required|string',\n        'list' =\u003e 'array|min:1',\n    ];\n}\n```\n\nValidating the data can be done as follows:\n\n```php\n    $dataObject = new YourDataObject();\n\n    // validate() returns a boolean, false if it does not follow the rules\n    if ( ! $dataObject-\u003evalidate()) {\n\n        $messages = $dataObject-\u003emessages();\n\n        // messages() returns a standard Laravel MessageBag\n        dd( $messages-\u003efirst() );\n    }\n```\n\nMessages are a `MessageBag` object generated by the Validator (whatever is behind the Laravel Facade `Validator`).\n\n\n## Validation\n\nTo use the extra Validation features for nested DataObject validation rules and better array validation, load the ServiceProvider for this package.\nThis is the only reason to load the ServiceProvider; this package does not itself require the Provider to function.\n\nAdd this line of code to the providers array located in your `config/app.php` file:\n\n```php\n    Czim\\DataObject\\DataObjectServiceProvider::class,\n```\n\nNote that this will rebind the `Validator` facade, so if you have done this yourself, you may instead want to use the provided validation Traits to add to your own extended validator class.\n\nRead more information about [the validation (traits) here](VALIDATION.md).\n\n\n## Castable Data Object\n\nYou may opt to extend the `Czim\\DataObject\\CastableDataObject`.\nBesides the standard features, this also includes the possibility of casting its properties to scalar values or (nested) data objects. This works similarly to Eloquent's `$casts` property (with some minor differences).\n\nBy overriding a protected `casts()` method, it is possible to set a cast type per attribute key:\n\n```php\n\u003c?php\nprotected function casts()\n{\n    return [\n        'check' =\u003e 'boolean',\n        'count' =\u003e 'integer',\n        'price' =\u003e 'float',\n    ];\n}\n```\n\nThis has the effect of casting each property to its set type before returning it.\n\n```php\n\u003c?php\n$object = new YourDataObject([\n    'check' =\u003e 'truthy value',\n    'price' =\u003e 45,\n]);\n\n$object-\u003echeck;     // true\n$object['price'];   // 45.0\n$object-\u003ecount;     // 0 (instead of null)\n```\n\n### toArray Casting\n\nCast types are also applied for the `toArray()` method of the data object.\nThis means that unset properties will be present in the array as their default value (`false` for boolean, `0.0` for float, etc.).\n\nTo disable this behaviour, set `$castToArray` to `false`.\nThis will entirely disable casting values on `toArray()`.\n\n\n### Nested Object Casting\n\nMore useful than scalar-casting, is object casting. This will allow you to create a tree of nested objects that, if set, can be invoked fluently.\n\nGiven casts that are set as follows:\n\n```php\n\u003c?php\nclass RootDataObject extends \\Czim\\DataObject\\CastableDataObject\n{\n    protected function casts()\n    {\n        return [\n            'some_object' =\u003e YourDataObject::class,\n            'object_list' =\u003e YourDataObject::class . '[]',\n        ];\n    }\n}\n```\n\nAnd with the following data example, you can access the data by property:\n\n```php\n\u003c?php\n$data = [\n    'some_object' =\u003e [\n        'type' =\u003e 'peaches',\n    ],\n    'object_list' =\u003e [\n        ['type' =\u003e 'cucumbers'],\n        ['type' =\u003e 'sherry'],\n    ],\n];\n\n$object = new RootDataobject($data);\n\n$object-\u003esome_object;           // instance of YourDataObject\n$object-\u003esome_object-\u003etype;     // peaches\n$object-\u003eobject_list[1]-\u003etype;  // sherry\n```\n\nNote that unset or `null` values will return `null`, not an empty data object. Non-array values will cause exceptions to be thrown on being interpreted as data objects.\n\nThis behaviour can be changed by setting the `$castUnsetObjects` property to `true`: unset attributes with an object cast will then be cast as an empty instance of that object class.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n\n## Credits\n\n- [Coen Zimmerman][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/czim/laravel-dataobject.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/czim/laravel-dataobject.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/czim/laravel-dataobject\n[link-downloads]: https://packagist.org/packages/czim/laravel-dataobject\n[link-author]: https://github.com/czim\n[link-contributors]: ../../contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczim%2Flaravel-dataobject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczim%2Flaravel-dataobject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczim%2Flaravel-dataobject/lists"}