Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/n7olkachev/php-simple-delegator
Create decorators with ease
https://github.com/n7olkachev/php-simple-delegator
decorators php simpledelegator
Last synced: about 2 months ago
JSON representation
Create decorators with ease
- Host: GitHub
- URL: https://github.com/n7olkachev/php-simple-delegator
- Owner: n7olkachev
- Created: 2017-08-28T13:15:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-08-28T16:56:29.000Z (over 7 years ago)
- Last Synced: 2024-04-16T09:06:07.341Z (9 months ago)
- Topics: decorators, php, simpledelegator
- Language: PHP
- Homepage:
- Size: 7.81 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# Simple Delegator
[![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 $fillable = [
'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 UserPresenter($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)