briedis / api-builder
Installs: 2 140
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: >=7.1
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0
- mockery/mockery: ~1.0
Requires (Dev)
- phpunit/phpunit: ~6.0|~8.5.12
README
Library helps you build a documentation for your api, and you can even use it to validate request parameters
Usage
Laravel 4
- Add composer dependency
"briedis/api-builder": "~1.0"
and runcomposer update
- Add service provider
Briedis\ApiBuilder\ApiBuilderLaravel4ServiceProvider
- Publish assets public directory:
php artisan asset:publish briedis/api-builder
Laravel 5
- Add composer dependency
"briedis/api-builder": "~1.0"
and runcomposer update
- Add service provider
Briedis\ApiBuilder\ApiBuilderLaravel5ServiceProvider
- Publish assets to public directory:
php artisan vendor:publish --force --provider="Briedis\ApiBuilder\ApiBuilderLaravel5ServiceProvider"
(force means that existing files will be overwritten)
Request class
use Briedis\ApiBuilder\Method; use Briedis\ApiBuilder\StructureBuilder; class ExampleGetUserRequest extends Method{ const URI = 'user/get'; const METHOD = 'GET'; public $title = 'User information'; public $description = 'Get user by given ids. One or multiple users can be fetched at once'; public function getRequest(){ return new GetUsersStructure; } public function getResponse(){ return (new StructureBuilder) ->struct('users', new UserStructure, 'Array with user objects')->multiple(); } }
Structure classes
use Briedis\ApiBuilder\StructureBuilder; use Briedis\ApiBuilder\StructureInterface; class GetUsersStructure implements StructureInterface { /** * Get the structure object * @return \Briedis\ApiBuilder\StructureBuilder */ public function getStructure(){ return (new StructureBuilder) ->int('userId', 'Array of user ids you want to fetch')->multiple() ->int('offset', 'For paging purposes')->optional() ->int('count', 'Amount of users to fetch. Defaults to 20')->optional(); } } class UserStructure implements StructureInterface{ /** * Get User structure object * @return StructureBuilder */ public function getStructure(){ return (new StructureBuilder('User')) ->int('id', 'Unique identifier') ->str('username', 'Nickname that will be used in the system') ->str('firstName', 'Users first name') ->str('lastName', 'Users last name') ->str('gender', 'M - male, F - female')->values(['M', 'F'])->optional() ->int('signature', 'Provide your favorite quote or something, if you want')->optional() ->struct('location', new LocationStructure, 'Location object for the user')->optional() ->int('createdAt', 'Unix timestamp, when user has registered'); } }
Outputting
$presenter = new \Briedis\ApiBuilder\Presenter([ new ExampleGetUserRequest, // Add request class instances as needed ]); $presenter->setDomain('http://example/api/v1')); echo $presenter->render();
TODO
- Automatic route generating