├── .github └── workflows │ └── tests.yml ├── LICENSE ├── composer.json └── src ├── Decorator.php ├── PresentableInterface.php ├── Presenter.php ├── PresenterServiceProvider.php └── View ├── Factory.php └── View.php /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | tests: 7 | runs-on: ubuntu-latest 8 | 9 | strategy: 10 | fail-fast: true 11 | matrix: 12 | php: [8.1, 8.2, 8.3] 13 | 14 | name: PHP ${{ matrix.php }} 15 | 16 | steps: 17 | - name: Checkout code 18 | uses: actions/checkout@v3 19 | 20 | - name: Setup PHP 21 | uses: shivammathur/setup-php@v2 22 | with: 23 | php-version: ${{ matrix.php }} 24 | coverage: none 25 | 26 | - name: Install Composer dependencies 27 | run: | 28 | composer install --prefer-dist --no-interaction --no-progress 29 | composer install -d examples/laravel-9.x --prefer-dist --no-interaction --no-progress 30 | composer run post-root-package-install -d examples/laravel-9.x 31 | composer run post-create-project-cmd -d examples/laravel-9.x 32 | ${{ matrix.php != '8.0' && 'composer install -d examples/laravel-10.x --prefer-dist --no-interaction --no-progress' || ''}} 33 | ${{ matrix.php != '8.0' && 'composer run post-root-package-install -d examples/laravel-10.x' || ''}} 34 | ${{ matrix.php != '8.0' && 'composer run post-create-project-cmd -d examples/laravel-10.x' || ''}} 35 | 36 | - name: Execute tests 37 | run: | 38 | vendor/bin/phpunit 39 | (cd examples/laravel-9.x && vendor/bin/phpunit) 40 | ${{ matrix.php != '8.0' && '(cd examples/laravel-10.x && vendor/bin/phpunit)' || ''}} 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Robert Clancy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "robclancy/presenter", 3 | "description": "Decorate your objects using presenters. Primarily to keep presentation logic out of your models.", 4 | "type": "library", 5 | "keywords": ["decorator", "presenter", "laravel"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Robbo", 10 | "email": "robbo.clancy@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "php": "^8.1" 15 | }, 16 | "require-dev": { 17 | "illuminate/view": ">=9.0", 18 | "phpunit/phpunit": "^10.0", 19 | "mockery/mockery": "~1.3.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Robbo\\Presenter\\": "src/" 24 | } 25 | }, 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "Robbo\\Presenter\\PresenterServiceProvider" 30 | ] 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Decorator.php: -------------------------------------------------------------------------------- 1 | getPresenter(); 20 | } 21 | 22 | if (is_array($value) or ($value instanceof IteratorAggregate and $value instanceof ArrayAccess)) { 23 | foreach ($value as $k => $v) { 24 | $value[$k] = $this->decorate($v); 25 | } 26 | } 27 | 28 | return $value; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/PresentableInterface.php: -------------------------------------------------------------------------------- 1 | object = $object; 30 | } 31 | 32 | /** 33 | * Get the decorator, if none exists then use the default. Underscores here to avoid conflicts 34 | * if a presenter or object needs to use "getDecorator". 35 | * 36 | * @var \Robbo\Presenter\Decorator 37 | */ 38 | protected function __getDecorator() 39 | { 40 | if (is_null(static::$__decorator)) { 41 | static::$__decorator = new Decorator(); 42 | } 43 | 44 | return static::$__decorator; 45 | } 46 | 47 | /** 48 | * This is so you can extend the decorator and inject it into the presenter at the class level so the 49 | * new decorator will be used for nested presenters. Method name should be "setDecorator" however 50 | * like above I want to make conflicts less likely. 51 | * 52 | * @param \Robbo\Presenter\Decorator 53 | */ 54 | public static function setExtendedDecorator(Decorator $decorator) 55 | { 56 | static::$__decorator = $decorator; 57 | } 58 | 59 | /** 60 | * Get the object we are wrapping. 61 | * 62 | * @return mixed 63 | */ 64 | public function getObject() 65 | { 66 | return $this->object; 67 | } 68 | 69 | /* 70 | * This will be called when isset() is called via array access. 71 | * 72 | * @param mixed $offset 73 | * @return boolean 74 | */ 75 | public function offsetExists($offset): bool 76 | { 77 | // We only check isset on the array, if it is an object we return true as the object could be overloaded 78 | if (! is_array($this->object)) { 79 | return true; 80 | } 81 | 82 | if ($method = $this->getPresenterMethodFromVariable($offset)) { 83 | $result = $this->$method(); 84 | 85 | return isset($result); 86 | } 87 | 88 | return isset($this->object[$offset]); 89 | } 90 | 91 | /** 92 | * Add ability to access properties like an array. 93 | * 94 | * @param mixed $offset 95 | * @return mixed 96 | */ 97 | public function offsetGet($offset): mixed 98 | { 99 | return $this->__get($offset); 100 | } 101 | 102 | /** 103 | * Set variable or key value using array access. 104 | * 105 | * @param mixed $offset 106 | * @param mixed $value 107 | */ 108 | public function offsetSet($offset, $value): void 109 | { 110 | if (is_array($this->object)) { 111 | $this->object[$offset] = $value; 112 | 113 | return; 114 | } 115 | 116 | $this->object->$offset = $value; 117 | } 118 | 119 | /** 120 | * Unset a variable or key value using array access. 121 | * 122 | * @param mixed $offset 123 | */ 124 | public function offsetUnset($offset): void 125 | { 126 | if (is_array($this->object)) { 127 | unset($this->object[$offset]); 128 | 129 | return; 130 | } 131 | 132 | unset($this->object->$offset); 133 | } 134 | 135 | /** 136 | * Pass any unknown variable calls to present{$variable} or fall through to the injected object. 137 | * 138 | * @param string $var 139 | * @return mixed 140 | */ 141 | public function __get($var) 142 | { 143 | if ($method = $this->getPresenterMethodFromVariable($var)) { 144 | return $this->$method(); 145 | } 146 | 147 | return $this->__getDecorator()->decorate(is_array($this->object) ? $this->object[$var] : $this->object->$var); 148 | } 149 | 150 | /** 151 | * Pass any unknown methods through to the inject object. 152 | * 153 | * @param string $method 154 | * @param array $arguments 155 | * @return mixed 156 | */ 157 | public function __call($method, $arguments) 158 | { 159 | if (is_object($this->object)) { 160 | $value = call_user_func_array([$this->object, $method], $arguments); 161 | 162 | return $this->__getDecorator()->decorate($value); 163 | } 164 | 165 | throw new \BadMethodCallException("Method {$method} does not exist."); 166 | } 167 | 168 | /** 169 | * Allow ability to run isset() on a variable. 170 | * 171 | * @param string $name 172 | * @return bool 173 | */ 174 | public function __isset($name) 175 | { 176 | if ($method = $this->getPresenterMethodFromVariable($name)) { 177 | $result = $this->$method(); 178 | 179 | return isset($result); 180 | } 181 | 182 | if (is_array($this->object)) { 183 | return isset($this->object[$name]); 184 | } 185 | 186 | return isset($this->object->$name); 187 | } 188 | 189 | /** 190 | * Allow to unset a variable through the presenter. 191 | * 192 | * @param string $name 193 | */ 194 | public function __unset($name) 195 | { 196 | if (is_array($this->object)) { 197 | unset($this->object[$name]); 198 | 199 | return; 200 | } 201 | 202 | unset($this->object->$name); 203 | } 204 | 205 | /** 206 | * Fetch the 'present' method name for the given variable. 207 | * 208 | * @param string $variable 209 | * @return string|null 210 | */ 211 | protected function getPresenterMethodFromVariable($variable) 212 | { 213 | $method = 'present'.str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $variable))); 214 | if (method_exists($this, $method)) { 215 | return $method; 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/PresenterServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerDecorator(); 22 | 23 | $this->registerFactory(); 24 | } 25 | 26 | /** 27 | * Register the decorator. If you want to extend the decorator you would basically copy 28 | * what this method does in start/global.php or your own service provider. 29 | */ 30 | public function registerDecorator() 31 | { 32 | $this->app->singleton('presenter.decorator', function ($app) { 33 | $decorator = new Decorator(); 34 | 35 | // This isn't really doing anything here however if you want to extend the decorator 36 | // with your own instance then you need to do it like this in your own service 37 | // provider or in start/global.php. 38 | Presenter::setExtendedDecorator($decorator); 39 | 40 | return $decorator; 41 | }); 42 | } 43 | 44 | /** 45 | * Copied from the view service provider... 46 | * 47 | * Register the view factory. 48 | */ 49 | public function registerFactory() 50 | { 51 | $this->app->singleton('view', function ($app) { 52 | // Next we need to grab the engine resolver instance that will be used by the 53 | // factory. The resolver will be used by a factory to get each of 54 | // the various engine implementations such as plain PHP or Blade engine. 55 | $resolver = $app['view.engine.resolver']; 56 | 57 | $finder = $app['view.finder']; 58 | 59 | $factory = new View\Factory($resolver, $finder, $app['events'], $app['presenter.decorator']); 60 | 61 | // We will also set the container instance on this view factory since the 62 | // view composers may be classes registered in the container, which allows 63 | // for great testable, flexible composers for the application developer. 64 | $factory->setContainer($app); 65 | 66 | $factory->share('app', $app); 67 | 68 | return $factory; 69 | }); 70 | } 71 | 72 | /** 73 | * Get the services provided by the provider. 74 | * 75 | * @return array 76 | */ 77 | public function provides() 78 | { 79 | return []; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/View/Factory.php: -------------------------------------------------------------------------------- 1 | presenterDecorator = $decorator; 27 | 28 | parent::__construct($engines, $finder, $events); 29 | } 30 | 31 | /** 32 | * Create a new view instance from the given arguments. 33 | * 34 | * @param string $view 35 | * @param string $path 36 | * @param \Illuminate\Contracts\Support\Arrayable|array $data 37 | * @return \Illuminate\Contracts\View\View 38 | */ 39 | protected function viewInstance($view, $path, $data) 40 | { 41 | return new View($this, $this->getEngineFromPath($path), $view, $path, $this->decorate($data)); 42 | } 43 | 44 | /** 45 | * Add a piece of shared data to the factory. 46 | * 47 | * @param string $key 48 | * @param mixed $value 49 | */ 50 | public function share($key, $value = null) 51 | { 52 | if (! is_array($key)) { 53 | return parent::share($key, $this->decorate($value)); 54 | } 55 | 56 | return parent::share($this->decorate($key)); 57 | } 58 | 59 | /** 60 | * Decorate an object with a presenter. 61 | * 62 | * @param mixed $value 63 | * @return mixed 64 | */ 65 | public function decorate($value) 66 | { 67 | return $this->presenterDecorator->decorate($value); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/View/View.php: -------------------------------------------------------------------------------- 1 | factory->decorate($key)); 20 | } 21 | 22 | return parent::with($key, $this->factory->decorate($value)); 23 | } 24 | } 25 | --------------------------------------------------------------------------------