-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roman Kinyakin
committed
May 14, 2015
1 parent
0d095d8
commit c15f8cf
Showing
2 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
## Attributable trait | ||
## Attributable | ||
|
||
[![Build Status](https://travis-ci.org/rkgrep/attributable.svg)](https://travis-ci.org/rkgrep/attributable) | ||
[![Latest Stable Version](https://poser.pugx.org/rkgrep/attributable/v/stable.svg)](https://packagist.org/packages/rkgrep/attributable) | ||
|
@@ -7,7 +7,7 @@ | |
|
||
> **Note:** Original idea by Taylor Otwell in [Laravel Framework](https://github.com/laravel/framework). | ||
The trait is used to provide fast and elegant way to work with objects. | ||
The package includes traits which allow fluent and elegant way to work with internal object property arrays. | ||
|
||
## Installation | ||
|
||
|
@@ -26,8 +26,20 @@ class Foo { | |
} | ||
```` | ||
|
||
````php | ||
class Bar { | ||
|
||
use rkgrep\Fillable; | ||
|
||
} | ||
```` | ||
|
||
## Usage | ||
|
||
### Attributable trait | ||
|
||
`Attributable` provides different access and assignment ways. | ||
|
||
Assign internal variables via property or method call | ||
|
||
````php | ||
|
@@ -68,14 +80,58 @@ Chain methods for fast assignment | |
$user->first_name('John')->last_name('Doe')->admin(); | ||
```` | ||
|
||
### Fillable trait | ||
|
||
`Fillable` provides chaining assignment of variables or groups of variables. | ||
|
||
Mass assign atributes with `fill` method | ||
|
||
````php | ||
$user->fill(['name' => 'Admin', 'email' => '[email protected]']); | ||
```` | ||
|
||
Overwrite or reassign control via second parameter | ||
|
||
````php | ||
$user->fill(['name' => 'John Doe']); // Name changed, email remains untouched | ||
$user->fill(['email' => '[email protected]'], false); // Disabled merging - old values are dropped | ||
```` | ||
|
||
Fill specific properties with `with` method | ||
|
||
````php | ||
$user->with('password', md5('password')); | ||
```` | ||
|
||
Prevent overriding with third parameter | ||
|
||
````php | ||
$user->with('password', '', false); // Password remains untouched | ||
```` | ||
|
||
Assign multiple variables | ||
|
||
````php | ||
$user->with(['friends' => ['Mike', 'Dave'], 'girlfriend' => 'Jane']); | ||
$user->with(['siblings' => [], 'girlfriend' => 'Mary'], null, false); // Overriding disabled - only siblings are touched | ||
```` | ||
|
||
Chain method calls | ||
|
||
````php | ||
$post->fill(['title' => 'Lorem Ipsum'])->with('views', 5)->with('likes', 3); | ||
```` | ||
|
||
## Interfaces | ||
|
||
Any class with `Attributable` trait applied implements `ArrayAccess` and `JsonSerializable`. | ||
If you use *illuminate/support* package you can also apply `Arrayable` and `Jsonable` interfaces. | ||
|
||
````php | ||
class Bar implements ArrayAccess, JsonSerializable, Arrayable, Jsonable { | ||
|
||
use rkgrep\Attributable; | ||
|
||
} | ||
|
||
$bar = new Bar(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters