Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
n7olkachev committed Aug 28, 2017
1 parent abf1baa commit 6eb81d1
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 4 deletions.
33 changes: 33 additions & 0 deletions .scrutinizer.yml
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]
17 changes: 17 additions & 0 deletions .travis.yml
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
115 changes: 115 additions & 0 deletions README.MD
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)
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "n7olkachev/php-simple-decorator",
"name": "n7olkachev/php-simple-delegator",
"description": "Simple decorator trait for PHP",
"homepage": "https://github.com/n7olkachev/php-simple-decorator",
"homepage": "https://github.com/n7olkachev/php-simple-delegator",
"keywords":
[
"websecret",
Expand Down
4 changes: 2 additions & 2 deletions src/SimpleDelegator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ abstract protected function delegatee();

public function __get($key)
{
return $this->delegatee()->{$key};
return $this->delegatee()->$key;
}

public function __set($key, $value)
{
return $this->delegatee()->{$key} = $value;
return $this->delegatee()->$key = $value;
}

public function __call($key, $args)
Expand Down

0 comments on commit 6eb81d1

Please sign in to comment.