├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── SerializerSwitch.php ├── Serializers ├── ArraySerializer.php └── DataArraySerializer.php └── ServiceProvider.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | node_modules/ 3 | npm-debug.log 4 | 5 | # Laravel 4 specific 6 | bootstrap/compiled.php 7 | app/storage/ 8 | 9 | # Laravel 5 & Lumen specific 10 | public/storage 11 | public/hot 12 | storage/*.key 13 | .env.*.php 14 | .env.php 15 | .env 16 | Homestead.yaml 17 | Homestead.json 18 | 19 | # Rocketeer PHP task runner and deployment package. https://github.com/rocketeers/rocketeer 20 | .rocketeer/ 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Yu Li 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 | # Dingo Serializer Switch 2 | 3 | This is a middleware for dingo/api to switch serializer 4 | 5 | If a resource's relationship is null, we can return `$this->null()` in a transformer. But default serializer return [], I think null is better. 6 | Also when pagination has no links, default is [], I wish to return null. So I write two serializer to fix this. 7 | 8 | If you want to use default serializer just use `default_array` or `default_data_array`. 9 | 10 | ## Installation 11 | 12 | ### Laravel 13 | 14 | - `composer require liyu/dingo-serializer-switch` 15 | 16 | 17 | ### Lumen 18 | 19 | *bootstrap/app.php* 20 | ```php 21 | $app->routeMiddleware([ 22 | // ... 23 | 'serializer' => Liyu\Dingo\SerializerSwitch::class, 24 | ]); 25 | ``` 26 | 27 | ## Usage 28 | 29 | ``` 30 | $api->version('v1', 31 | ['middleware' => 'serializer:array'], 32 | function ($api) { 33 | }); 34 | 35 | $api->version('v2', 36 | ['middleware' => 'serializer'], 37 | function ($api) { 38 | }); 39 | 40 | $api->version('v3', 41 | ['middleware' => 'serializer:data_array'], 42 | function ($api) { 43 | }); 44 | ``` 45 | 46 | default key is data_array, so v2 v3 is same. 47 | 48 | ``` 49 | 'default_array' => 'League\Fractal\Serializer\ArraySerializer', 50 | 'default_data_array' => 'League\Fractal\Serializer\DataArraySerializer', 51 | 'json_api' => 'League\Fractal\Serializer\JsonApiSerializer', 52 | 53 | 'array' => 'Liyu\Dingo\Serializers\ArraySerializer', 54 | 'data_array' => 'Liyu\Dingo\Serializers\DataArraySerializer', 55 | ``` 56 | 57 | ## License 58 | [MIT LICENSE](https://github.com/liyu001989/dingo-serializer-switch/blob/master/LICENSE) 59 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "liyu/dingo-serializer-switch", 3 | "description": "A middleware to switch dingo serializer", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Yu Li", 8 | "email": "liyu001989@gmail.com" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=7.0", 13 | "dingo/api": "^2.0|^3.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Liyu\\Dingo\\": "src" 18 | } 19 | }, 20 | "extra": { 21 | "laravel": { 22 | "providers": [ 23 | "Liyu\\Dingo\\ServiceProvider" 24 | ] 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/SerializerSwitch.php: -------------------------------------------------------------------------------- 1 | 'League\Fractal\Serializer\ArraySerializer', 11 | 'default_data_array' => 'League\Fractal\Serializer\DataArraySerializer', 12 | 'json_api' => 'League\Fractal\Serializer\JsonApiSerializer', 13 | 14 | // change null resource return null instead of [] 15 | 'array' => 'Liyu\Dingo\Serializers\ArraySerializer', 16 | 'data_array' => 'Liyu\Dingo\Serializers\DataArraySerializer', 17 | ]; 18 | 19 | protected function getDriver($name) 20 | { 21 | $name = array_key_exists($name, $this->drivers) ? $name : 'data_array'; 22 | return $this->drivers[$name]; 23 | } 24 | 25 | /** 26 | * Handle an incoming request. 27 | * 28 | * @param \Illuminate\Http\Request $request 29 | * @param \Closure $next 30 | * @return mixed 31 | */ 32 | public function handle($request, Closure $next, $name = 'data_array') 33 | { 34 | $driver = $this->getDriver($name); 35 | 36 | app('Dingo\Api\Transformer\Factory')->setAdapter(function ($app) use ($driver) { 37 | $fractal = new \League\Fractal\Manager; 38 | $serializer = new $driver; 39 | 40 | $fractal->setSerializer($serializer); 41 | return new \Dingo\Api\Transformer\Adapter\Fractal($fractal); 42 | }); 43 | 44 | return $next($request); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Serializers/ArraySerializer.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Liyu\Dingo\Serializers; 13 | 14 | use League\Fractal\Pagination\PaginatorInterface; 15 | use League\Fractal\Serializer\ArraySerializer as BaseSerializer; 16 | 17 | class ArraySerializer extends BaseSerializer 18 | { 19 | /** 20 | * Serialize null resource. 21 | * 22 | * @return array|null 23 | */ 24 | public function null(): ?array 25 | { 26 | return null; 27 | } 28 | 29 | /** 30 | * Serialize the paginator. 31 | * 32 | * @param PaginatorInterface $paginator 33 | * 34 | * @return array 35 | */ 36 | public function paginator(PaginatorInterface $paginator): array 37 | { 38 | $currentPage = (int) $paginator->getCurrentPage(); 39 | $lastPage = (int) $paginator->getLastPage(); 40 | 41 | $pagination = [ 42 | 'total' => (int) $paginator->getTotal(), 43 | 'count' => (int) $paginator->getCount(), 44 | 'per_page' => (int) $paginator->getPerPage(), 45 | 'current_page' => $currentPage, 46 | 'total_pages' => $lastPage, 47 | ]; 48 | 49 | $pagination['links']['previous'] = null; 50 | $pagination['links']['next'] = null; 51 | 52 | if ($currentPage > 1) { 53 | $pagination['links']['previous'] = $paginator->getUrl($currentPage - 1); 54 | } 55 | 56 | if ($currentPage < $lastPage) { 57 | $pagination['links']['next'] = $paginator->getUrl($currentPage + 1); 58 | } 59 | 60 | if (!array_filter($pagination['links'])) { 61 | $pagination['links'] = null; 62 | } 63 | 64 | return ['pagination' => $pagination]; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Serializers/DataArraySerializer.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | namespace Liyu\Dingo\Serializers; 13 | 14 | use League\Fractal\Pagination\PaginatorInterface; 15 | use League\Fractal\Serializer\DataArraySerializer as BaseSerializer; 16 | 17 | class DataArraySerializer extends BaseSerializer 18 | { 19 | /** 20 | * Serialize null resource. 21 | * 22 | * @return array|null 23 | */ 24 | public function null(): ?array 25 | { 26 | return ['data' => null]; 27 | } 28 | 29 | /** 30 | * Serialize the paginator. 31 | * 32 | * @param PaginatorInterface $paginator 33 | * 34 | * @return array 35 | */ 36 | public function paginator(PaginatorInterface $paginator): array 37 | { 38 | $currentPage = (int) $paginator->getCurrentPage(); 39 | $lastPage = (int) $paginator->getLastPage(); 40 | 41 | $pagination = [ 42 | 'total' => (int) $paginator->getTotal(), 43 | 'count' => (int) $paginator->getCount(), 44 | 'per_page' => (int) $paginator->getPerPage(), 45 | 'current_page' => $currentPage, 46 | 'total_pages' => $lastPage, 47 | ]; 48 | 49 | $pagination['links']['previous'] = null; 50 | $pagination['links']['next'] = null; 51 | 52 | if ($currentPage > 1) { 53 | $pagination['links']['previous'] = $paginator->getUrl($currentPage - 1); 54 | } 55 | 56 | if ($currentPage < $lastPage) { 57 | $pagination['links']['next'] = $paginator->getUrl($currentPage + 1); 58 | } 59 | 60 | if (!array_filter($pagination['links'])) { 61 | $pagination['links'] = null; 62 | } 63 | 64 | return ['pagination' => $pagination]; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['router']->aliasMiddleware('serializer', SerializerSwitch::class); 15 | } 16 | } 17 | --------------------------------------------------------------------------------