Download the PHP package pxc/json-api-client without Composer
On this page you can find all versions of the php package pxc/json-api-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pxc/json-api-client
More information about pxc/json-api-client
Files in pxc/json-api-client
Package json-api-client
Short Description A copy of swisnl/json-api-client with headers configuration in request.
License MIT
Informations about the package json-api-client
{ json:api } Client
A PHP package for mapping remote {json:api} resources to Eloquent like models and collections.
A copy of json-api-client
@todo this documentation needs to be updated.
Upgrade from 1.0.x to 1.2.x
The dependency swisnl/json-api-client
has been upgraded from '0.10.x' to '0.20.x', supporting Lumen 5.8. In your ServiceProvider
- Replace
use Swis\JsonApi\Client\Interfaces\ParserInterface;
withuse Swis\JsonApi\Client\Interfaces\ResponseParserInterface;
- Replace
use Swis\JsonApi\Client\ItemDocumentSerializer;
withuse PXC\JsonApi\Client\ItemDocumentSerializerInterface;
- Change the other part of the file accordingly.
Installation
N.B. Make sure you have installed a HTTP Client before you install this package or install one at the same time e.g. composer require swisnl/json-api-client php-http/guzzle6-adapter
.
HTTP Client
We are decoupled from any HTTP messaging client with the help of PHP-HTTP.
This requires another package providing php-http/client-implementation.
To use Guzzle 6, for example, simply require php-http/guzzle6-adapter
:
Laravel Service Provider
If you are using Laravel < 5.5 or have disabled package auto discover, you must add the service provider to your config/app.php
file:
Getting started
You can simply require the client as a dependency and use it in your class. This allows you to, for example, make a repository that uses the DocumentClient:
Configuration
The following is the default configuration provided by this package:
Publish Configuration
If you would like to make changes to the default configuration, publish and edit the configuration file:
Clients
This package offers two clients; DocumentClient
and Client
.
DocumentClient
This is the client that you would generally use.
Per the JSON API spec, all requests and responses are documents.
Therefore, this client always expects a \Swis\JsonApi\Client\Interfaces\DocumentInterface
as input when posting data and always returns this same interface.
This can be a plain Document
when there is no data, an ItemDocument
for an item, a CollectionDocument
for a collection or an InvalidResponseDocument
when the server responds with a non 2xx response.
The DocumentClient
follows the following steps internally:
- Send the request using your HTTP client;
- Use art4/json-api-client to parse and validate the response;
- Create the correct document instance;
- Hydrate every item by using the item model registered with the
TypeMapper
or a\Swis\JsonApi\Client\Item
as fallback; - Hydrate all relationships;
- Add meta data to the document such as errors, links and meta.
Client
This client is a more low level client and can be used, for example, for posting binary data such as images.
It can take everything your request factory takes as input data and returns the 'raw' \Psr\Http\Message\ResponseInterface
wrapped in a \Swis\JsonApi\Client\Response
.
It does not parse or validate the response or hydrate items!
Items
By default, all items are an instance of \Swis\JsonApi\Client\Item
.
The Item
extends jenssegers/model, which provides a Laravel Eloquent-like base class.
Please see it's documentation about the features it provides.
You can define your own models by extending \Swis\JsonApi\Client\Item
or by implementing the \Swis\JsonApi\Client\Interfaces\ItemInterface
yourself.
This can be useful if you want to define, for example, hidden attributes, casts or get/set mutators.
If you use custom models, you must register them with the TypeMapper.
Relations
On top of jenssegers/model, this package has implemented Laravel Eloquent-like relations. These relations provide a fluent interface to retrieve the related items. There are currently four relations available:
HasOneRelation
HasManyRelation
MorphToRelation
MorphToManyRelation
Please see the following example about defining the relationships:
Collections
This package uses Laravel Collections as a wrapper for item arrays.
TypeMapper
All custom models must be registered with the TypeMapper
.
This TypeMapper
maps, as the name suggests, JSON API types to custom item models.
Mapping types
You can manually register items with the \Swis\JsonApi\Client\TypeMapper
or use the supplied \Swis\JsonApi\Client\Providers\TypeMapperServiceProvider
:
Service Provider
The \Swis\JsonApi\Client\Providers\ServiceProvider
registers the TypeMapper
, JsonApi\Parser
and both clients; Client
and DocumentClient
.
Each section can be overwritten to allow extended customization.
Bind TypeMapper
The service provider registers the TypeMapper
as a singleton so your entire application has the same mappings available.
Bind Clients
The service provider registers the Client
and DocumentClient
to your application.
By default it uses php-http/discovery to find a HTTP client and message factory.
You can specify your own message factory by overwriting the getMessageFactory()
method and your own HTTP client by overwriting the getHttpClient()
method.
The first should return a message factory and the latter should return a HTTP client, both implementing HTTPlug.
These methods are the perfect place to add extra options to your HTTP client or register a mock HTTP client for your tests:
N.B. This example uses our swisnl/php-http-fixture-client when in testing environment. This package allows you to easily mock requests with static fixtures. Definitely worth a try!
If you register your own service provider and use package auto discover, don't forget to exclude this package in your package.json
:
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
Contributing
Please see CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.
SWIS
SWIS is a web agency from Leiden, the Netherlands. We love working with open source software.
All versions of json-api-client with dependencies
swisnl/json-api-client Version ^0.20.0
php-http/message Version ^1.7
php-http/client-implementation Version ^1.0
php-http/discovery Version ^1.0