├── .editorconfig ├── .styleci.yml ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Macros ├── Error.php ├── Message.php ├── Pdf.php └── Success.php ├── ResponseMacroInterface.php ├── ResponseMacros.php └── ResponseMacrosServiceProvider.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | linting: true 4 | 5 | disabled: 6 | - single_class_element_per_statement 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Appstract 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 13 | > all 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 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Response Macros 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/appstract/laravel-response-macros.svg?style=flat-square)](https://packagist.org/packages/appstract/laravel-response-macros) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/appstract/laravel-response-macros.svg?style=flat-square)](https://packagist.org/packages/appstract/laravel-response-macros) 5 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 6 | [![Build Status](https://img.shields.io/travis/appstract/laravel-response-macros/master.svg?style=flat-square)](https://travis-ci.org/appstract/laravel-response-macros) 7 | 8 | This package is a collection of custom response macros that you can re-use in a variety of your routes and controllers. 9 | 10 | ## Installation 11 | 12 | You can install the package via composer: 13 | 14 | ``` bash 15 | composer require appstract/laravel-response-macros 16 | ``` 17 | 18 | ## Usage 19 | 20 | ### Message 21 | ``` php 22 | return response()->message('hello world!', 200); 23 | ``` 24 | 25 | Result 26 | 27 | ``` json 28 | { 29 | "message": "hello world!" 30 | } 31 | ``` 32 | With the Http `Status Code: 200` 33 | 34 | ### Error 35 | ``` php 36 | return response()->error('Something went wrong', $statuscode = 400); 37 | ``` 38 | 39 | Result 40 | 41 | ``` json 42 | { 43 | "message": "Something went wrong" 44 | } 45 | ``` 46 | With the Http `Status Code: 400` 47 | 48 | ### Success 49 | ``` php 50 | return response()->success(['some' => 'data'], $statuscode = 200); 51 | ``` 52 | 53 | Result 54 | 55 | ``` json 56 | { 57 | "data": {"some": "data"} 58 | } 59 | ``` 60 | With the Http `Status Code: 200` 61 | 62 | ### PDF 63 | 64 | Creates a (downloadable) PDF response from PDF contents. 65 | 66 | ``` php 67 | return response()->pdf($pdfData, 'filename.pdf', $download = false); 68 | ``` 69 | 70 | ## Testing 71 | 72 | ``` bash 73 | $ composer test 74 | ``` 75 | 76 | ## Contributing 77 | 78 | Contributions are welcome, [thanks to y'all](https://github.com/appstract/laravel-blade-directives/graphs/contributors) :) 79 | 80 | ## About Appstract 81 | 82 | Appstract is a small team from The Netherlands. We create (open source) tools for webdevelopment and write about related subjects on [Medium](https://medium.com/appstract). You can [follow us on Twitter](https://twitter.com/teamappstract), [buy us a beer](https://www.paypal.me/teamappstract/10) or [support us on Patreon](https://www.patreon.com/appstract). 83 | 84 | ## License 85 | 86 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 87 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "appstract/laravel-response-macros", 3 | "description": "Extra Response Macro's for Laravel", 4 | "keywords": ["appstract", "laravel", "response", "macros", "php"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Bob Krijnen", 9 | "email": "hello@appstract.team", 10 | "homepage": "https://appstract.team", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.6", 16 | "laravel/framework": ">=5.4" 17 | }, 18 | "require-dev": { 19 | "phpunit/phpunit": "^5.7" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "Appstract\\ResponseMacros\\": "src" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "psr-4": { 28 | "Appstract\\ResponseMacros\\Test\\": "tests" 29 | } 30 | }, 31 | "scripts": { 32 | "test": "vendor/bin/phpunit" 33 | }, 34 | "config": { 35 | "sort-packages": true 36 | }, 37 | "extra": { 38 | "laravel": { 39 | "providers": [ 40 | "Appstract\\ResponseMacros\\ResponseMacrosServiceProvider" 41 | ] 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Macros/Error.php: -------------------------------------------------------------------------------- 1 | macro('error', function ($message = 'Bad Request', $status = 400) use ($factory) { 12 | return $factory->make([ 13 | 'errors' => [ 14 | 'message' => $message, 15 | ], 16 | ], $status); 17 | }); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Macros/Message.php: -------------------------------------------------------------------------------- 1 | macro('message', function ($message, $status) use ($factory) { 12 | return $factory->make([ 13 | 'message' => $message, 14 | ], $status); 15 | }); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Macros/Pdf.php: -------------------------------------------------------------------------------- 1 | macro('pdf', function ($pdf, $fileName, $download = false) use ($factory) { 12 | return $factory->make($pdf)->withHeaders(collect([ 13 | 'Content-Type' => 'application/pdf', 14 | ])->when($download, function ($collection) use ($fileName) { 15 | return $collection->put('Content-Disposition', 'attachment; filename="'.$fileName.'"'); 16 | })->toArray()); 17 | }); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Macros/Success.php: -------------------------------------------------------------------------------- 1 | macro('success', function ($data, $status = 200) use ($factory) { 12 | return $factory->make([ 13 | 'data' => $data, 14 | ], $status); 15 | }); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/ResponseMacroInterface.php: -------------------------------------------------------------------------------- 1 | macros = [ 22 | Macros\Message::class, 23 | Macros\Success::class, 24 | Macros\Error::class, 25 | Macros\Pdf::class, 26 | ]; 27 | 28 | $this->bindMacros($factory); 29 | } 30 | 31 | /** 32 | * Bind macros. 33 | * @param ResponseFactory $factory 34 | * @return void 35 | */ 36 | public function bindMacros($factory) 37 | { 38 | foreach ($this->macros as $macro) { 39 | (new $macro)->run($factory); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/ResponseMacrosServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->runningInConsole()) { 15 | // 16 | } 17 | } 18 | 19 | /** 20 | * Register the application services. 21 | */ 22 | public function register() 23 | { 24 | $this->app->make('Appstract\ResponseMacros\ResponseMacros'); 25 | } 26 | } 27 | --------------------------------------------------------------------------------