-
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
1 parent
abf1baa
commit 6eb81d1
Showing
5 changed files
with
169 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
filter: | ||
excluded_paths: [tests/*] | ||
checks: | ||
php: | ||
code_rating: true | ||
remove_extra_empty_lines: true | ||
remove_php_closing_tag: true | ||
remove_trailing_whitespace: true | ||
fix_use_statements: | ||
remove_unused: true | ||
preserve_multiple: false | ||
preserve_blanklines: true | ||
order_alphabetically: true | ||
fix_php_opening_tag: true | ||
fix_linefeed: true | ||
fix_line_ending: true | ||
fix_identation_4spaces: true | ||
fix_doc_comments: true | ||
tools: | ||
external_code_coverage: false | ||
php_analyzer: true | ||
php_code_coverage: false | ||
php_code_sniffer: | ||
config: | ||
standard: PSR2 | ||
filter: | ||
paths: ['src'] | ||
php_loc: | ||
enabled: true | ||
excluded_dirs: [vendor, tests] | ||
php_cpd: | ||
enabled: true | ||
excluded_dirs: [vendor, tests] |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
language: php | ||
|
||
php: | ||
- 7.0 | ||
- 7.1 | ||
|
||
env: | ||
matrix: | ||
- COMPOSER_FLAGS="--prefer-lowest" | ||
- COMPOSER_FLAGS="" | ||
|
||
before_script: | ||
- travis_retry composer self-update | ||
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source | ||
|
||
script: | ||
- phpunit |
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# Simple Delegator | ||
|
||
<p align="center"> | ||
<a href="https://websecret.by"><img src="https://websecret.by/images/logo-github.png" /></a> | ||
</p> | ||
|
||
[![Code quality](https://img.shields.io/scrutinizer/g/n7olkachev/php-simple-delegator.svg?style=flat-square)](https://scrutinizer-ci.com/g/n7olkachev/php-simple-delegator/) | ||
[![Licence](https://img.shields.io/packagist/l/n7olkachev/php-simple-delegator.svg?style=flat-square)](https://packagist.org/packages/n7olkachev/php-simple-delegator) | ||
[![Build Status](https://travis-ci.org/n7olkachev/php-simple-delegator.svg?branch=master)](https://travis-ci.org/n7olkachev/php-simple-delegator) | ||
|
||
## Why? | ||
|
||
You can watch Jeffrey Way's video from Laracon US 2017 with usage example: https://streamacon.com/video/laracon-us-2017/day-2-jeffrey-way (35:00) | ||
|
||
## Examples | ||
|
||
Suppose, we want to create Presenter for our User model: | ||
|
||
```php | ||
class User extends Model | ||
{ | ||
protected $filterable = [ | ||
'name', | ||
]; | ||
} | ||
|
||
class UserPresenter | ||
{ | ||
use SimpleDelegator; | ||
|
||
protected $user; | ||
|
||
public function __construct($user) | ||
{ | ||
$this->user = $user; | ||
} | ||
|
||
protected function delegatee() | ||
{ | ||
return $this->user; | ||
} | ||
|
||
public function formattedName() | ||
{ | ||
return 'Decorated $this->user->name'; | ||
} | ||
} | ||
|
||
$decoratedUser = new UserDecorator($user); | ||
|
||
$decoratedUser->formattedName() // call to decorator method | ||
$decoratedUser->name // gets original name from User model | ||
``` | ||
|
||
Or, we want to add some additional behavior to our class (as in Jeffrey's example): | ||
|
||
```php | ||
class NotifyingThread | ||
{ | ||
protected $thread; | ||
|
||
public function __construct($thread) | ||
{ | ||
$this->thread = $thread; | ||
} | ||
|
||
public function addReply() | ||
{ | ||
$reply = $this->thread->addReply(); | ||
|
||
Notification::send( | ||
$reply->mentionedUsers(), | ||
new YouWereMentioned($reply) | ||
); | ||
} | ||
} | ||
|
||
$thread = new NotifyingThread($thread); | ||
|
||
$thread->addReply($data); // default logic + sending notification | ||
``` | ||
|
||
## Installation | ||
|
||
You can install the package via composer: | ||
|
||
``` bash | ||
composer require n7olkachev/php-simple-delegator | ||
``` | ||
|
||
Next, add SimpleDelegator trait to your Decorator class: | ||
|
||
```php | ||
use SimpleDelegator; | ||
``` | ||
|
||
## Testing | ||
|
||
``` bash | ||
$ composer test | ||
``` | ||
|
||
## Credits | ||
|
||
- [Nikita Tolkachev](https://github.com/n7olkachev) | ||
|
||
## Sponsored by | ||
|
||
https://websecret.by/ | ||
|
||
Web agency based in Minsk, Belarus | ||
|
||
## License | ||
|
||
The MIT License (MIT) |
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
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