├── .editorconfig ├── .styleci.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── DestroyTrait.php ├── IndexTrait.php ├── QueryTrait.php ├── ResourceTrait.php ├── SaveTrait.php ├── ShowTrait.php ├── StoreTrait.php ├── UpdateTrait.php └── ValidationTrait.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_size = 4 6 | indent_style = space 7 | end_of_line = lf 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.{yml,blade.php}] 15 | indent_size = 2 -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: recommended 2 | 3 | finder: 4 | exclude: 5 | - "tests" 6 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | We accept contributions via Pull Requests on [Github](https://github.com/marketplacehub/php-b2w-client). 6 | 7 | 8 | ## Pull Requests 9 | 10 | - **[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](http://pear.php.net/package/PHP_CodeSniffer). 11 | 12 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 13 | 14 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 15 | 16 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 17 | 18 | - **Create feature branches** - Don't ask us to pull from your master branch. 19 | 20 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 21 | 22 | - **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](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Juliano Bailão 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel - Easy Api 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | [![StyleCI][ico-styleci]][link-styleci] 7 | [![Coverage Status][ico-scrutinizer]][link-scrutinizer] 8 | [![Quality Score][ico-code-quality]][link-code-quality] 9 | [![Total Downloads][ico-downloads]][link-downloads] 10 | [![Donate][ico-donate]][link-donate] 11 | 12 | [ico-version]: https://img.shields.io/packagist/v/julianobailao/laravel-easy-api.svg?style=flat-square 13 | [ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square 14 | [ico-travis]: https://img.shields.io/travis/julianobailao/laravel-easy-api/master.svg?style=flat-square 15 | [ico-scrutinizer]:https://img.shields.io/scrutinizer/coverage/g/julianobailao/laravel-easy-api.svg?style=flat-square 16 | [ico-code-quality]:https://img.shields.io/scrutinizer/g/julianobailao/laravel-easy-api.svg?style=flat-square 17 | [ico-downloads]: https://img.shields.io/packagist/dt/julianobailao/laravel-easy-api.svg?style=flat-square 18 | [ico-styleci]: https://styleci.io/repos/80361872/shield 19 | [ico-donate]:https://img.shields.io/badge/Donate-PayPal-brightgreen.svg?style=flat-square 20 | 21 | [link-packagist]: https://packagist.org/packages/julianobailao/laravel-easy-api 22 | [link-travis]: https://travis-ci.org/julianobailao/laravel-easy-api 23 | [link-scrutinizer]: https://scrutinizer-ci.com/g/julianobailao/laravel-easy-api/?branch=master 24 | [link-code-quality]: https://scrutinizer-ci.com/g/julianobailao/laravel-easy-api/?branch=master 25 | [link-downloads]: https://packagist.org/packages/julianobailao/laravel-easy-api 26 | [link-styleci]: https://styleci.io/repos/80361872 27 | [link-donate]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=LDRJCTGY2YXYJ 28 | 29 | This package will helps you build as quickly as possible your APIs, using pre-configured traits and resource controllers, [see the wiki for more information](https://github.com/julianobailao/laravel-easy-api/wiki). 30 | 31 | ## Install 32 | 33 | Via Composer 34 | 35 | ```bash 36 | $ composer require julianobailao/laravel-easy-api 37 | ``` 38 | 39 | ## Documentation 40 | 41 | Please, see the [wiki](https://github.com/julianobailao/laravel-easy-api/wiki) for the full documentation of this project. 42 | 43 | ## Basic Usage 44 | 45 | ### Create a Model: 46 | Create a model and configure it has you wish. 47 | 48 | ``` php 49 | 50 | namespace App; 51 | 52 | use Illuminate\Database\Eloquent\Model; 53 | 54 | class Donkey extends Model 55 | { 56 | // 57 | } 58 | ``` 59 | 60 | ### Create a controller 61 | Create a controller, and use the ResourceTrait of this package. Exists one trait for each method from resource controller equivalent, if you want use a especific methods, and not all of then, read about [Independent Methods](#). 62 | 63 | ``` php 64 | 65 | namespace App\Http\Controllers; 66 | 67 | use JulianoBailao\LaravelEasyApi\ResourceTrait; 68 | 69 | class DonkeyController extends Controller 70 | { 71 | use ResourceTrait; 72 | } 73 | ``` 74 | 75 | ### Route 76 | Configure your route like a resource controller: 77 | 78 | ``` php 79 | Route::resource('donkeys', 'DonkeyController', ['except' => ['create', 'edit']]); 80 | ``` 81 | 82 | ### It's runing! 83 | You have a resource controller with the index, show, store, update and destroy methods, runing for model Donkey, in the /donkeys route. 84 | 85 | Please, see the [wiki](https://github.com/julianobailao/laravel-easy-api/wiki) for the full documentation of this project. 86 | 87 | ## License 88 | 89 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 90 | 91 | ## Donate 92 | Support this project and others, via [PayPal](link-donate). 93 | 94 | [![Donate][ico-donate]][link-donate] 95 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "julianobailao/laravel-easy-api", 3 | "type": "library", 4 | "description": "Laravel Easy Api Helper.", 5 | "keywords": [ 6 | "juliano bailao", 7 | "api", 8 | "easy", 9 | "laravel", 10 | "php" 11 | ], 12 | "homepage": "https://github.com/julianobailao/laravel-easy-api", 13 | "license": "MIT", 14 | "authors": [ 15 | { 16 | "name": "Juliano Bailão", 17 | "email": "julianobailao@gmail.com", 18 | "homepage": "http://github.com/julianobailao", 19 | "role": "Developer" 20 | } 21 | ], 22 | "require": { 23 | "php": "~5.6|~7.0" 24 | }, 25 | "require-dev": { 26 | "phpunit/phpunit" : "~4.0||~5.0", 27 | "scrutinizer/ocular": "~1.1", 28 | "squizlabs/php_codesniffer": "~2.3", 29 | "laravel/laravel": "dev-develop", 30 | "orchestra/testbench": "~3.4||~3.3" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "JulianoBailao\\LaravelEasyApi\\": "src" 35 | } 36 | }, 37 | "autoload-dev": { 38 | "psr-4": { 39 | "JulianoBailao\\LaravelEasyApi\\Tests\\": "tests" 40 | } 41 | }, 42 | "scripts": { 43 | "test": "phpunit", 44 | "format": "phpcbf --standard=psr2 src/" 45 | }, 46 | "extra": { 47 | "branch-alias": { 48 | "dev-master": "0.1-dev" 49 | } 50 | }, 51 | "config": { 52 | "sort-packages": true 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/DestroyTrait.php: -------------------------------------------------------------------------------- 1 | destroyQuery($query, $id); 18 | } 19 | 20 | /** 21 | * Make the destroy query. 22 | * 23 | * @param Model $query 24 | * @param int $id 25 | * 26 | * @return bool 27 | */ 28 | protected function destroyQuery($query, $id) 29 | { 30 | $query = $this->getModel()->findOrFail($id); 31 | 32 | return $query->delete(); 33 | } 34 | 35 | /** 36 | * Override it to transform the destroy response. 37 | * 38 | * @param bool $query 39 | * 40 | * @return array 41 | */ 42 | protected function transformDestroyResponse($response) 43 | { 44 | return $this->destroyResponse($response); 45 | } 46 | 47 | /** 48 | * The destroy response. 49 | * 50 | * @param bool $response 51 | * 52 | * @return array 53 | */ 54 | public function destroyResponse($response) 55 | { 56 | return [ 57 | 'deleted' => $response, 58 | ]; 59 | } 60 | 61 | /** 62 | * The resource controller destroy method. 63 | * 64 | * @param int $id 65 | * 66 | * @return mixed 67 | */ 68 | public function destroy($id) 69 | { 70 | $deleted = $this->transformDestroyQuery($this->query, $id); 71 | $response = $this->transformDestroyResponse($deleted); 72 | 73 | return response()->json($response); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/IndexTrait.php: -------------------------------------------------------------------------------- 1 | where(function ($query) use ($searchTerm) { 20 | foreach ($this->getColumns() as $field) { 21 | $query->orWhere($field, '=', $searchTerm); 22 | $query->orWhere($field, 'like', '%'.$searchTerm.'%'); 23 | } 24 | }); 25 | } 26 | 27 | return $indexQuery; 28 | } 29 | 30 | /** 31 | * Override it to transform the index query. 32 | * 33 | * @param Model $query 34 | * @param Request $request 35 | * 36 | * @return Paginator 37 | */ 38 | protected function transformIndexQuery($query, Request $request) 39 | { 40 | return $this->indexQuery($query, $request); 41 | } 42 | 43 | /** 44 | * Make the index query. 45 | * 46 | * @param Model $query 47 | * @param Request $request 48 | * 49 | * @return Paginator 50 | */ 51 | protected function indexQuery($query, Request $request) 52 | { 53 | $selectFields = method_exists($this, 'selectFields') ? $this->selectFields() : '*'; 54 | $query = $query->select($selectFields); 55 | $query = $this->filter($query, $request->get('filter')); 56 | $data = $query->paginate($request->get('per_page') ?: 100); 57 | 58 | return $data; 59 | } 60 | 61 | /** 62 | * Overide it to transform the index response. 63 | * 64 | * @param array $data 65 | * 66 | * @return array 67 | */ 68 | protected function transformIndexResponse(array $data) 69 | { 70 | return $this->indexResponse($data); 71 | } 72 | 73 | /** 74 | * The index response. 75 | * 76 | * @param array $data 77 | * 78 | * @return array 79 | */ 80 | protected function indexResponse(array $data) 81 | { 82 | $response = []; 83 | $items = $data['data']; 84 | unset($data['data']); 85 | $response['metadata'] = $data; 86 | $response['items'] = $items; 87 | 88 | return $response; 89 | } 90 | 91 | /** 92 | * The index method from resource controller. 93 | * 94 | * @param Request $request 95 | * 96 | * @return mixed 97 | */ 98 | public function index(Request $request) 99 | { 100 | $query = $this->transformIndexQuery($this->getModel(), $request); 101 | $data = $this->transformIndexResponse($query->toArray()); 102 | 103 | return response()->json($data); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/QueryTrait.php: -------------------------------------------------------------------------------- 1 | modelName) || $this->modelName === null) { 24 | $this->modelName = str_replace('Controller', null, class_basename($this)); 25 | } 26 | 27 | return $this->modelName; 28 | } 29 | 30 | /** 31 | * Gets the model Object. 32 | * 33 | * @return mixed 34 | */ 35 | protected function getModel() 36 | { 37 | if (!isset($this->modelNameSpace) || $this->modelNameSpace === null) { 38 | $this->modelNameSpace = 'App\\'; 39 | } 40 | 41 | return app($this->modelNameSpace.$this->getModelName()); 42 | } 43 | 44 | /** 45 | * Gets the table columns. 46 | * 47 | * @return array 48 | */ 49 | protected function getColumns() 50 | { 51 | return Schema::getColumnListing($this->getModel()->getTable()); 52 | } 53 | 54 | /** 55 | * Gets the query builder object. 56 | * 57 | * @return Illuminate\Database\Eloquent\Builder 58 | */ 59 | protected function getQuery() 60 | { 61 | return $this->query; 62 | } 63 | 64 | /** 65 | * Sets the query builder object. 66 | * 67 | * @param mixed 68 | * 69 | * @return mixed 70 | */ 71 | protected function setQuery($builder) 72 | { 73 | return $this->query = $builder; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/ResourceTrait.php: -------------------------------------------------------------------------------- 1 | saveQuery($query, $request); 20 | } 21 | 22 | /** 23 | * Override it to transform the update query. 24 | * 25 | * @param Model $query 26 | * @param Request $request 27 | * @param int $id 28 | * 29 | * @return mixed 30 | */ 31 | protected function transformUpdateQuery($query, Request $request, $id) 32 | { 33 | return $this->saveQuery($query, $request, $id); 34 | } 35 | 36 | /** 37 | * Make the default save store/update query. 38 | * 39 | * @param Model $query 40 | * @param Request $request 41 | * @param int $id 42 | * 43 | * @return mixed 44 | */ 45 | protected function saveQuery($query, Request $request, $id = null) 46 | { 47 | $data = ($id !== null) ? $query->findOrFail($id) : new $query(); 48 | $data->fill($request->all()); 49 | $data->save(); 50 | 51 | return $query->findOrFail($data->id); 52 | } 53 | 54 | /** 55 | * Override it to transform the store response. 56 | * 57 | * @param Request $request 58 | * @param string $method 59 | * 60 | * @return array 61 | */ 62 | protected function transformStoreResponse(array $data, $method) 63 | { 64 | return $this->saveResponse($data, $method); 65 | } 66 | 67 | /** 68 | * Override it to transform the update response. 69 | * 70 | * @param Request $request 71 | * @param string $method 72 | * 73 | * @return array 74 | */ 75 | protected function transformUpdateResponse(array $data, $method, $id) 76 | { 77 | return $this->saveResponse($data, $method, $id); 78 | } 79 | 80 | /** 81 | * The store / update response. 82 | * 83 | * @param array $data 84 | * @param string $method 85 | * @param int $id 86 | * 87 | * @return array 88 | */ 89 | protected function saveResponse(array $data, $method, $id = null) 90 | { 91 | return [ 92 | 'id' => $id ?: $data['id'], 93 | 'method' => $method, 94 | 'data' => $data, 95 | ]; 96 | } 97 | 98 | /** 99 | * Save the data in the database from store and update method. 100 | * 101 | * @param Request $request 102 | * @param string $method 103 | * @param int $id 104 | * 105 | * @return mixed 106 | */ 107 | public function save(Request $request, $method, $id = null) 108 | { 109 | if (method_exists($this, 'validateRequest')) { 110 | $validation = $this->validateRequest($request, $method, $id); 111 | 112 | if ($validation !== true) { 113 | return $validation; 114 | } 115 | } 116 | 117 | $model = $this->getModel(); 118 | $query = $this->{'transform'.ucfirst($method).'Query'}($model, $request, $id); 119 | $data = $this->{'transform'.ucfirst($method).'Response'}($query->toArray(), $method, $id); 120 | 121 | return response()->json($data); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/ShowTrait.php: -------------------------------------------------------------------------------- 1 | showQuery($query, $id); 18 | } 19 | 20 | /** 21 | * Make the show query. 22 | * 23 | * @param Model $query 24 | * @param int $id 25 | * 26 | * @return bool 27 | */ 28 | protected function showQuery($query, $id) 29 | { 30 | return $query->findOrFail($id); 31 | } 32 | 33 | /** 34 | * Override it to transform the show response. 35 | * 36 | * @param array $data 37 | * @param int $id 38 | * 39 | * @return array 40 | */ 41 | protected function transformShowResponse(array $data, $id) 42 | { 43 | return $this->showResponse($data, $id); 44 | } 45 | 46 | /** 47 | * The show response. 48 | * 49 | * @param array $data 50 | * @param int $id 51 | * 52 | * @return array 53 | */ 54 | public function showResponse(array $data, $id) 55 | { 56 | return $data; 57 | } 58 | 59 | /** 60 | * Gets a json response with a specific model register. 61 | * 62 | * @param int $id 63 | * 64 | * @return mixed 65 | */ 66 | public function show($id) 67 | { 68 | $query = $this->transformShowQuery($this->getModel(), $id); 69 | $data = $this->transformShowResponse($query->toArray(), $id); 70 | 71 | return response()->json($data); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/StoreTrait.php: -------------------------------------------------------------------------------- 1 | save($request, 'store'); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/UpdateTrait.php: -------------------------------------------------------------------------------- 1 | save($request, 'update', $id); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/ValidationTrait.php: -------------------------------------------------------------------------------- 1 | all(), 43 | $this->validationRules($method, $id), 44 | $this->validationMessages($method, $id) 45 | ); 46 | 47 | if ($validator->fails()) { 48 | $rerrorData = [ 49 | 'error' => true, 50 | 'messages' => $validator->messages()->all(), 51 | ]; 52 | 53 | return response()->json($rerrorData, 422); 54 | } 55 | 56 | return true; 57 | } 58 | } 59 | --------------------------------------------------------------------------------