2017 © Pedro Peláez
 

library laravel-data-transfer-object

Data Transfer Objects with Attribute Casting and Date Mutators.

image

schulzefelix/laravel-data-transfer-object

Data Transfer Objects with Attribute Casting and Date Mutators.

  • Tuesday, November 29, 2016
  • by schulzefelix
  • Repository
  • 1 Watchers
  • 3 Stars
  • 4,886 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 31 % Grown

The README.md

Laravel Data Transfer Objects

Latest Version ![Software License][ico-license] Build Status StyleCI Latest Version on Packagist Total Downloads, (*1)

Brings Laravel Attribute Casting and Date Mutators to objects., (*2)

Install

Via Composer, (*3)

``` bash $ composer require schulzefelix/laravel-data-transfer-object, (*4)


## Usage - [Introduction](#introduction) - [Accessors & Mutators](#accessors-and-mutators) - [Defining An Accessor](#defining-an-accessor) - [Defining A Mutator](#defining-a-mutator) - [Date Mutators](#date-mutators) - [Attribute Casting](#attribute-casting) - [Serializing Objects & Collections](#serializing-objects-and-collections) - [Serializing To Arrays](#serializing-to-arrays) - [Serializing To JSON](#serializing-to-json) <a name="introduction"></a> ## Introduction Accessors and mutators allow you to format attribute values when you retrieve or set them on object instances. In addition to custom accessors and mutators, it can also automatically cast date fields to [Carbon](https://github.com/briannesbitt/Carbon) instances. <a name="accessors-and-mutators"></a> ## Accessors & Mutators <a name="defining-an-accessor"></a> ### Defining An Accessor To define an accessor, create a `getFooAttribute` method on your object where `Foo` is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the `first_name` attribute. The accessor will automatically be called when attempting to retrieve the value of the `slug` attribute: <?php namespace App; use SchulzeFelix\DataTransferObject\DataTransferObject; class Project extends DataTransferObject { /** * Get the objects URL friendly slug. * * @return string */ public function getSlugAttribute() { return str_slug($value); } } <a name="defining-a-mutator"></a> ### Defining A Mutator To define a mutator, define a `setFooAttribute` method on your object where `Foo` is the "studly" cased name of the column you wish to access. <?php namespace App; use SchulzeFelix\DataTransferObject\DataTransferObject; class Project extends DataTransferObject { /** * Set the objects's title. * * @param string $value * @return void */ public function serTitleAttribute($value) { $this->attributes['title'] = title_case($value); } } <a name="date-mutators"></a> ## Date Mutators By default, it will convert the `created_at` and `updated_at` columns to instances of [Carbon](https://github.com/briannesbitt/Carbon), which extends the PHP `DateTime` class to provide an assortment of helpful methods. You may customize which dates are automatically mutated: <?php namespace App; use SchulzeFelix\DataTransferObject\DataTransferObject; class Project extends DataTransferObject { /** * The attributes that should be mutated to dates. * * @var array */ protected $dates = [ 'date', 'deleted_at' ]; } As noted above, when retrieving attributes that are listed in your `$dates` property, they will automatically be cast to [Carbon](https://github.com/briannesbitt/Carbon) instances, allowing you to use any of Carbon's methods on your attributes: return $project->deleted_at->getTimestamp(); <a name="attribute-casting"></a> ## Attribute Casting The `$casts` property on your object provides a convenient method of converting attributes to common data types. The `$casts` property should be an array where the key is the name of the attribute being cast and the value is the type you wish to cast the attribute to. The supported cast types are: `integer`, `real`, `float`, `double`, `string`, `boolean`, `object`, `array`, `collection`, `date`, `datetime`, and `timestamp`. For example, let's cast the `is_index` attribute, which was assigned as string to a boolean value: <?php namespace App; use SchulzeFelix\DataTransferObject\DataTransferObject; class Project extends DataTransferObject { /** * The attributes that should be casted to native types. * * @var array */ protected $casts = [ 'is_index' => 'boolean', ]; } Now the `is_index` attribute will always be cast to a boolean when you access it, even if the underlying value was set as integer or string: if ($project->is_index) { // } <a name="serializing-objects-and-collections"></a> ## Serializing Objects & Collections <a name="serializing-to-arrays"></a> ### Serializing To Arrays To convert a object and its nested objects and collections to an array, you should use the `toArray` method. This method is recursive, so all attributes will be converted to arrays: return $project->toArray(); <a name="serializing-to-json"></a> ### Serializing To JSON To convert a object to JSON, you should use the `toJson` method. Like `toArray`, the `toJson` method is recursive, so all attributes and nested objects will be converted to JSON: return $project->toJson(); Alternatively, you may cast a object or collection to a string, which will automatically call the `toJson` method on the object or collection: return (string) $project; ## Change log Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. ## Testing ``` bash $ vendor/bin/phpunit

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*5)

Security

If you discover any security related issues, please email githubissues@schulze.co instead of using the issue tracker., (*6)

Credits

A great thanks to Taylor Otwell and all contributors for Laravel., (*7)

Modified source comes from the Eloquent Model., (*8)

Docs modified from Laravel Docs., (*9)

License

The MIT License (MIT). Please see License File for more information., (*10)

The Versions

29/11 2016

dev-master

9999999-dev https://github.com/schulzefelix/laravel-data-transfer-object

Data Transfer Objects with Attribute Casting and Date Mutators.

  Sources   Download

MIT

The Requires

 

The Development Requires

serialization dto data transfer object accessors mutators schulzefelix laravel-data-transfer-object felix attribute casting

29/11 2016

1.0.0

1.0.0.0 https://github.com/schulzefelix/laravel-data-transfer-object

Data Transfer Objects with Attribute Casting and Date Mutators.

  Sources   Download

MIT

The Requires

 

The Development Requires

serialization dto data transfer object accessors mutators schulzefelix laravel-data-transfer-object felix attribute casting

24/11 2016

0.2

0.2.0.0 https://github.com/schulzefelix/laravel-data-transfer-object

Data Transfer Objects with Attribute Casting and Date Mutators.

  Sources   Download

MIT

The Requires

 

The Development Requires

serialization dto data transfer object accessors mutators schulzefelix laravel-data-transfer-object felix attribute casting

22/11 2016

0.1

0.1.0.0 https://github.com/schulzefelix/laravel-data-transfer-object

Brings Laravel Attribute Casting and Date Mutators to objects.

  Sources   Download

MIT

The Requires

 

The Development Requires

schulzefelix laravel-data-transfer-object