PHPackages                             czim/laravel-dataobject - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. czim/laravel-dataobject

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

czim/laravel-dataobject
=======================

Basic validatable standardized data object.

3.0.1(4y ago)11142.9k↓43.6%7MITPHPPHP ^8.0CI failing

Since Sep 26Pushed 3y ago1 watchersCompare

[ Source](https://github.com/czim/laravel-dataobject)[ Packagist](https://packagist.org/packages/czim/laravel-dataobject)[ Docs](https://github.com/czim/laravel-dataobject)[ RSS](/packages/czim-laravel-dataobject/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (26)Used By (7)

Laravel Data Object
===================

[](#laravel-data-object)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f42f9cdb2ff8863298f21894d5680f81e40969906d403aa17e2453fe666779d3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f637a696d2f6c61726176656c2d646174616f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/czim/laravel-dataobject)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/3de9fd468606df39a9d93cb311a14c0170cf272741a926c5a731c460c29810e3/68747470733a2f2f7472617669732d63692e6f72672f637a696d2f6c61726176656c2d646174616f626a6563742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/czim/laravel-dataobject)[![Coverage Status](https://camo.githubusercontent.com/9bce1173c3414a6f59d919066b7cfaa9cfb70e4345adcca5f33407555099c695/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f637a696d2f6c61726176656c2d646174616f626a6563742f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/czim/laravel-dataobject?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/5e69e32c904854963e15dd8cd94a05af521589ac3376e599440ce2f63141e6dd/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f637a696d2f6c61726176656c2d646174616f626a6563742e737667)](https://packagist.org/packages/czim/laravel-dataobject)[![SensioLabsInsight](https://camo.githubusercontent.com/9b89314daecbe305440ec28f0768d6d2b9f31713a392311b302ac96ff847da79/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f33363966643464372d623264312d343433382d396530382d6537616435383662383163342f6d696e692e706e67)](https://insight.sensiolabs.com/projects/369fd4d7-b2d1-4438-9e08-e7ad586b81c4)

Basic framework for making standardized but flexible data objects. This provides a class (and interfaces) to use to standardize your dependencies or return values with a generic dataobject.

All this really is, is a data storage class with magic getters and setters. It is Arrayable, Jsonable and validatable.

Also provided are the means to make nested validation possible for data objects (containing futher data objects).

Version Compatibility
---------------------

[](#version-compatibility)

LaravelPackagePHP5.11.05.21.05.31.35.41.45.5 and up1.4, 2.09 and up3.08.0 and upInstall
-------

[](#install)

Via Composer

```
$ composer require czim/laravel-dataobject
```

Usage
-----

[](#usage)

Simply create your own extension of the base dataobject, and:

### Optionally add your own getters and setters

[](#optionally-add-your-own-getters-and-setters)

Basic stuff of course, but if you want your IDE to know what your objects contain, you can simply write getters and setters like this:

```
class TestDataObject extends \Czim\DataObject\AbstractDataObject
{
    public function getName($value)
    {
        $this->getAttribute('name');
    }

    public function setName($value)
    {
        $this->setAttribute('name', $value);
    }
}
```

Additionally, 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:

```
class TestDataObject extends \Czim\DataObject\AbstractDataObject
{
    protected $magicAssignment = false;

    ...
```

Attempts to set attributes on the DataObject by magic or array access will then throw an `UnassignableAttributeException`.

### Optionally add validation rules for attributes

[](#optionally-add-validation-rules-for-attributes)

```
class YourDataObject extends \Czim\DataObject\AbstractDataObject
{
    protected $rules = [
        'name' => 'required|string',
        'list' => 'array|min:1',
    ];
}
```

Validating the data can be done as follows:

```
    $dataObject = new YourDataObject();

    // validate() returns a boolean, false if it does not follow the rules
    if ( ! $dataObject->validate()) {

        $messages = $dataObject->messages();

        // messages() returns a standard Laravel MessageBag
        dd( $messages->first() );
    }
```

Messages are a `MessageBag` object generated by the Validator (whatever is behind the Laravel Facade `Validator`).

Validation
----------

[](#validation)

To use the extra Validation features for nested DataObject validation rules and better array validation, load the ServiceProvider for this package. This is the only reason to load the ServiceProvider; this package does not itself require the Provider to function.

Add this line of code to the providers array located in your `config/app.php` file:

```
    Czim\DataObject\DataObjectServiceProvider::class,
```

Note 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.

Read more information about [the validation (traits) here](VALIDATION.md).

Castable Data Object
--------------------

[](#castable-data-object)

You may opt to extend the `Czim\DataObject\CastableDataObject`. Besides 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).

By overriding a protected `casts()` method, it is possible to set a cast type per attribute key:

```
