├── .github └── workflows │ └── main.yml ├── .gitignore ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Console │ ├── PresentableCommand.php │ └── stubs │ │ └── custom-presenter.php.stub ├── HasPresentable.php ├── PresentableServiceProvider.php └── Presenter.php └── tests ├── HasPresentableTest.php └── PresenterTest.php /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: [push] 4 | 5 | jobs: 6 | test: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - name: Checkout code 10 | uses: actions/checkout@v2 11 | 12 | - name: Setup PHP 13 | uses: shivammathur/setup-php@v2 14 | with: 15 | php-version: 8.1 16 | 17 | - name: Install dependencies 18 | run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist 19 | 20 | - name: Execute tests 21 | run: vendor/bin/phpunit 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | .phpunit.result.cache 3 | composer.lock -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-presentable` will be documented in this file 4 | 5 | ## 0.2.0 6 | 7 | - Update support for Laravel Framework v9 8 | ## 0.0.1 9 | 10 | - Initial release 11 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) The Hive Team 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Presentable 2 | 3 | [![Total Downloads](https://img.shields.io/packagist/dt/datacreativa/laravel-presentable.svg?style=flat-square)](https://packagist.org/packages/datacreativa/laravel-presentable) 4 | ![GitHub Actions](https://github.com/datacreativa/laravel-presentable/actions/workflows/main.yml/badge.svg) 5 | 6 | ![Banner](https://banners.beyondco.de/Laravel%20Presentable.png?theme=light&packageManager=composer+require&packageName=datacreativa%2Flaravel-presentable&pattern=bubbles&style=style_1&description=Create+presenters+for+Eloquent+Models&md=1&showWatermark=1&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg) 7 | 8 | This package allows the information to be presented in a different way by means of methods that can be defined in the model's presenter class. 9 | 10 | ## Installation 11 | 12 | You can install the package via composer: 13 | 14 | | Laravel | Version | 15 | |---------|---------| 16 | | 6, 7, 8 | 1.0.0 | 17 | | 9 | 1.1.0 | 18 | | 10 | 1.2.0 | 19 | | 11 | 1.3.0 | 20 | 21 | ```bash 22 | composer require datacreativa/laravel-presentable 23 | ``` 24 | 25 | ## Usage 26 | 27 | Use the `HasPresentable` trait in any Eloquent Model, you must also add the `$presenter` property. 28 | 29 | ```php 30 | 31 | use App\Models\Presenters\UserPresenter; 32 | use TheHiveTeam\Presentable\HasPresentable; 33 | 34 | class User extends Model 35 | { 36 | use HasPresentable; 37 | 38 | protected $presenter = UserPresenter::class; 39 | } 40 | 41 | ``` 42 | 43 | You can generate a class presenter with this command: 44 | 45 | ```bash 46 | php artisan make:presenter UserPresenter 47 | ``` 48 | 49 | In this class you can define any method, for example: 50 | 51 | ```php 52 | namespace App\Models\Presenters; 53 | 54 | use TheHiveTeam\Presentable\Presenter; 55 | 56 | class UserPresenter extends Presenter 57 | { 58 | public function name() 59 | { 60 | return ucwords($this->model->name); 61 | } 62 | } 63 | ``` 64 | 65 | Now you have available the `present()` method in Eloquent Model. 66 | 67 | ```php 68 | $user = (new User)->setAttribute('name', 'john doe'); 69 | $user->present()->name; 70 | // John Doe 71 | ``` 72 | 73 | Enjoy it! 74 | 75 | ### Testing 76 | 77 | ```bash 78 | composer test 79 | ``` 80 | 81 | ### Changelog 82 | 83 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 84 | 85 | ## Contributing 86 | 87 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 88 | 89 | ## Credits 90 | 91 | - [Jonathan Zarate](https://github.com/zaratedev) 92 | - [All Contributors](../../contributors) 93 | 94 | ## License 95 | 96 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 97 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "datacreativa/laravel-presentable", 3 | "description": "Create presenters for Eloquent Models", 4 | "keywords": [ 5 | "laravel", 6 | "presentable" 7 | ], 8 | "homepage": "https://github.com/datacreativa/laravel-presentable", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Jonathan Zarate", 14 | "email": "zaratedev@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": ">=7.2|^8.0", 20 | "illuminate/support": "~6.0|~7.0|~8.0|~9.0|^10.0|^11.0" 21 | }, 22 | "require-dev": { 23 | "orchestra/testbench": "^6.0|^7.0|^8.0|^9.0", 24 | "phpunit/phpunit": "^8.0|^9.0|^10.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "TheHiveTeam\\Presentable\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "TheHiveTeam\\Presentable\\Tests\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "test": "vendor/bin/phpunit" 38 | }, 39 | "config": { 40 | "sort-packages": true 41 | }, 42 | "extra": { 43 | "laravel": { 44 | "providers": [ 45 | "TheHiveTeam\\Presentable\\PresentableServiceProvider" 46 | ] 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | tests 7 | 8 | 9 | 10 | 11 | src/ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Console/PresentableCommand.php: -------------------------------------------------------------------------------- 1 | model->name); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/HasPresentable.php: -------------------------------------------------------------------------------- 1 | presenter) { 26 | throw new Exception('Please set the $presenter property to your presenter path.'); 27 | } 28 | 29 | if (! class_exists($this->presenter)) { 30 | throw new Exception("The presenter class [{$this->presenter}] does not exists."); 31 | } 32 | 33 | if (! $this->presenterInstance) { 34 | $this->presenterInstance = Container::getInstance()->make($this->presenter); 35 | 36 | if (! $this->presenterInstance instanceof Presenter) { 37 | throw new Exception("The presenter [{$this->presenter}] must be an instance of [".Presenter::class.']'); 38 | } 39 | 40 | $this->presenterInstance->setModel($this); 41 | } 42 | 43 | return $this->presenterInstance; 44 | } 45 | } -------------------------------------------------------------------------------- /src/PresentableServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 13 | $this->commands([ 14 | PresentableCommand::class, 15 | ]); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Presenter.php: -------------------------------------------------------------------------------- 1 | model = $model; 19 | } 20 | 21 | /** 22 | * Allow for property-style retrieval. 23 | * 24 | * @param mixed $property 25 | * @return mixed 26 | */ 27 | public function __get($property) 28 | { 29 | if (method_exists($this, $property)) { 30 | return $this->$property(); 31 | } 32 | 33 | return $this->model->$property; 34 | } 35 | 36 | /** 37 | * Provide compatibility for the 'or' syntax in blade templates. 38 | * 39 | * @param mixed $property 40 | * @return bool 41 | */ 42 | public function __isset($property): bool 43 | { 44 | return method_exists($this, $property); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/HasPresentableTest.php: -------------------------------------------------------------------------------- 1 | present(); 23 | } catch (Exception $exception) { 24 | $this->assertEquals('Please set the $presenter property to your presenter path.', $exception->getMessage()); 25 | 26 | return; 27 | } 28 | 29 | $this->fail('Did not throw the expected exception'); 30 | } 31 | 32 | /** @test */ 33 | public function it_throws_an_exception_id_the_presenter_class_does_not_exist() 34 | { 35 | $model = new class extends Model 36 | { 37 | use HasPresentable; 38 | 39 | protected $presenter = 'UnExistentClass'; 40 | }; 41 | 42 | try { 43 | $model->present(); 44 | } catch (Exception $exception) { 45 | $this->assertEquals('The presenter class [UnExistentClass] does not exists.', $exception->getMessage()); 46 | 47 | return; 48 | } 49 | 50 | $this->fail('Did not throw the expected exception'); 51 | } 52 | 53 | /** @test */ 54 | public function it_throws_an_exception_id_the_presenter_class_does_not_extend_presenter() 55 | { 56 | $model = new class extends Model 57 | { 58 | use HasPresentable; 59 | 60 | protected $presenter = \stdClass::class; 61 | }; 62 | 63 | try { 64 | $model->present(); 65 | } catch (Exception $exception) { 66 | $this->assertEquals( 67 | 'The presenter ['.\stdClass::class.'] must be an instance of ['.Presenter::class.']', 68 | $exception->getMessage() 69 | ); 70 | 71 | return; 72 | } 73 | 74 | $this->fail('Did not throw the expected exception'); 75 | } 76 | 77 | /** @test */ 78 | public function it_returns_null_when_attribute_is_not_set() 79 | { 80 | $model = new class extends Model 81 | { 82 | use HasPresentable; 83 | 84 | protected $presenter = FakePresenter::class; 85 | }; 86 | 87 | $this->assertNull($model->present()->name); 88 | } 89 | 90 | /** @test */ 91 | public function it_returns_unaltered_attribute_when_presenter_method_does_not_exist() 92 | { 93 | $model = new class extends Model 94 | { 95 | use HasPresentable; 96 | 97 | protected $attributes = ['email' => 'john@example.com']; 98 | protected $presenter = FakePresenter::class; 99 | }; 100 | 101 | $this->assertEquals('john@example.com', $model->present()->email); 102 | } 103 | 104 | /** @test */ 105 | public function it_returns_modified_attribute_when_presenter_method_exists() 106 | { 107 | $model = new class extends Model 108 | { 109 | use HasPresentable; 110 | 111 | protected $attributes = ['price' => 1000]; 112 | protected $presenter = FakePresenter::class; 113 | }; 114 | 115 | $this->assertEquals('$ 1,000 MXN', $model->present()->price); 116 | } 117 | } 118 | 119 | class FakePresenter extends Presenter 120 | { 121 | public function price() 122 | { 123 | $price = number_format($this->model->price); 124 | 125 | return "$ {$price} MXN"; 126 | } 127 | } -------------------------------------------------------------------------------- /tests/PresenterTest.php: -------------------------------------------------------------------------------- 1 | assertEquals('-', $class->present()->title); 23 | } 24 | } 25 | 26 | class TestPresenter extends Presenter 27 | { 28 | public function title() 29 | { 30 | return $this->model->title ?? '-'; 31 | } 32 | } 33 | --------------------------------------------------------------------------------