Download the PHP package nilportugues/json-api without Composer
On this page you can find all versions of the php package nilportugues/json-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nilportugues/json-api
More information about nilportugues/json-api
Files in nilportugues/json-api
Package json-api
Short Description Serializer transformers outputting valid API responses in JSON, JSON API and HAL+JSON API formats.
License MIT
Homepage http://nilportugues.com
Informations about the package json-api
JSON API Transformer & Server Helpers
- Installation
- Transfomer Classes
- Server Classes
- JSON API Request object
- Request Object
- JSON API Response objects
- Action Objects
Installation
Use Composer to install the package:
Transfomer Classes
Given a PHP Object, and a series of mappings, the JSON API Transformer will represent the given data following the http://jsonapi.org
specification.
For instance, given the following piece of code, defining a Blog Post and some comments:
And a Mapping series of classes implementing JsonApiMapping
interface.
Calling the transformer will output a valid JSON API response using the correct formatting:
Output (formatted):
Server Classes
JSON API Request object
JSON API comes with its Request class, framework agnostic, implementing the PSR-7 Request Interface.
Using this request object will provide you access to all the interactions expected in a JSON API:
Defined Query Parameters:
- &fields[resource]=field1,field2 will only show the specified fields for a given resource.
- &include=resource show the relationship for a given resource.
- &include=resource.resource2 show the relationship field for those depending on resource2.
- &sort=field1,-field2 sort by field2 as DESC and field1 as ASC.
- &sort=-field1,field2 sort by field1 as DESC and field2 as ASC.
- &page[number] will return the current page elements in a page-based pagination strategy.
- &page[size] will return the total amout of elements in a page-based pagination strategy.
- &page[limit] will return the limit in a offset-based pagination strategy.
- &page[offset] will return the offset value in a offset-based pagination strategy.
- &page[cursor] will return the cursor value in a cursor-based pagination strategy.
- &filter will return data passed in the filter param.
Request Object
Given the query parameters listed above, Request implements helper methods that parse and return data already prepared.
JSON API Response objects
Because the JSON API specification lists a set of behaviours, specific Response objects are provided for successful and error cases.
Success
NilPortugues\Api\JsonApi\Http\Response\Response
NilPortugues\Api\JsonApi\Http\Response\ResourceUpdated
NilPortugues\Api\JsonApi\Http\Response\ResourceAccepted
NilPortugues\Api\JsonApi\Http\Response\ResourceCreated
NilPortugues\Api\JsonApi\Http\Response\ResourceDeleted
NilPortugues\Api\JsonApi\Http\Response\ResourceProcessing
Error
NilPortugues\Api\JsonApi\Http\Response\BadRequest
NilPortugues\Api\JsonApi\Http\Response\ResourceConflicted
NilPortugues\Api\JsonApi\Http\Response\ResourceNotFound
NilPortugues\Api\JsonApi\Http\Response\TooManyRequests
NilPortugues\Api\JsonApi\Http\Response\UnprocessableEntity
NilPortugues\Api\JsonApi\Http\Response\UnsupportedAction
Forbidden Access
It is also possible to fire a Forbidden
response by throwing the following Exception in your code:
NilPortugues\Api\JsonApi\Server\Actions\Exceptions\ForbiddenException
Access control logic is not provided.
Action Objects
Having Request and Response objects and Transformers, it just makes sense to have a set of classes that tie them all together into something more powerful: Actions.
Provided actions are:
NilPortugues\Api\JsonApi\Server\Actions\CreateResource
NilPortugues\Api\JsonApi\Server\Actions\DeleteResource
NilPortugues\Api\JsonApi\Server\Actions\GetResource
NilPortugues\Api\JsonApi\Server\Actions\ListResource
NilPortugues\Api\JsonApi\Server\Actions\PatchResource
NilPortugues\Api\JsonApi\Server\Actions\PutResource
All actions share a get
method to run the Resource.
These get
methods will expect in all cases one or more callables
. This has been done to avoid coupling with any library or interface and being able to extend it.
Quality
To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit.
This library attempts to comply with PSR-1, PSR-2, PSR-4 and PSR-7.
If you notice compliance oversights, please send a patch via Pull Request.
Contribute
Contributions to the package are always welcome!
- Report any bugs or issues you find on the issue tracker.
- You can grab the source code at the package's Git repository.
Support
Get in touch with me using one of the following means:
- Emailing me at [email protected]
- Opening an Issue
- Using Gitter:
Authors
License
The code base is licensed under the MIT license.
All versions of json-api with dependencies
nilportugues/api-transformer Version <=3.0.0
symfony/psr-http-message-bridge Version ^0.2|^0.3|^1.0