├── LICENSE.md ├── README.md ├── composer.json └── src ├── Bootstrap.php ├── Decorator.php ├── Presenter.php ├── exceptions └── PresenterException.php ├── rest └── Serializer.php └── traits └── PresentableTrait.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ivan Kudinov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii2 View Presenters 2 | ============= 3 | 4 | So you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) 5 | is displayed from the view. 6 | 7 | * Should that logic be hard-coded into the view? **No.** 8 | * Should we instead store the logic in the model? **No again!** 9 | 10 | Instead, leverage view presenters. That's what they're for! This package provides one such implementation. 11 | 12 | ## Installation 13 | 14 | Run the [Composer](http://getcomposer.org/download/) command to install the latest stable version: 15 | 16 | ```bash 17 | composer require frostealth/yii2-presenter @stable 18 | ``` 19 | 20 | ## Usage 21 | 22 | The first step is to store your presenters somewhere - anywhere. 23 | These will be simple objects that do nothing more than format data, as required. 24 | 25 | Here's an example of a presenter. 26 | 27 | ```php 28 | namespace app\presenters; 29 | 30 | use app\models\User; 31 | use frostealth\yii2\presenter\Presenter; 32 | 33 | /** 34 | * Class ConcreteEntityPresenter 35 | * 36 | * @property User $entity 37 | * 38 | * @property-read string $firstName 39 | * @property-read string $lastName 40 | * @property-read string $fullName 41 | * @property-read string $birthDate 42 | */ 43 | class UserPresenter extends Presenter 44 | { 45 | /** 46 | * @return string 47 | */ 48 | public function getFullName() 49 | { 50 | return implode(' ', [$this->firstName, $this->lastName]); 51 | } 52 | 53 | /** 54 | * @return string 55 | */ 56 | public function getBirthDate() 57 | { 58 | return date('y.M.d', $this->entity->birthDate); 59 | } 60 | 61 | /** 62 | * @inheritdoc 63 | * @see \yii\base\Arrayable::fields() 64 | * @link http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#fields 65 | */ 66 | public function fields() 67 | { 68 | $fields = parent::fields(); 69 | $fields[] = 'fullName'; 70 | 71 | return $fields; 72 | } 73 | } 74 | ``` 75 | 76 | Next, on your entity, pull in the `frostealth\yii2\presenter\traits\PresentableTrait` trait, 77 | which will automatically instantiate your presenter class. 78 | 79 | Here's an example of an presentable model. 80 | 81 | ```php 82 | namespace app\models; 83 | 84 | use app\presenters\UserPresenter; 85 | use frostealth\presenter\interfaces\PresentableInterface; 86 | use frostealth\yii2\presenter\traits\PresentableTrait; 87 | 88 | /** 89 | * Class User 90 | * 91 | * @property string $firstName 92 | * @property string $lastName 93 | * @property string $birthDate 94 | * @property string $passwordHash 95 | * @property string $passwordResetToken 96 | * 97 | * @method UserPresenter presenter() 98 | */ 99 | class User extends ActiveRecord implements PresentableInterface 100 | { 101 | use PresentableTrait; 102 | 103 | /** 104 | * @inheritdoc 105 | * @see \yii\base\Arrayable::fields() 106 | * @link http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#fields 107 | */ 108 | public function fields() 109 | { 110 | $fields = parent::fields(); 111 | unset($fields['passwordHash'], $fields['passwordResetToken']); 112 | 113 | return $fields; 114 | } 115 | 116 | /** 117 | * @return string|array 118 | */ 119 | protected function getPresenterClass() 120 | { 121 | return 'app\presenters\UserPresenter'; 122 | } 123 | } 124 | ``` 125 | 126 | Now, within your view, you can do: 127 | 128 | ```php 129 |