Download the PHP package woohoolabs/yang without Composer

On this page you can find all versions of the php package woohoolabs/yang. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?
woohoolabs/yang
Rate from 1 - 5
Rated 5.00 based on 1 reviews

Informations about the package yang

Woohoo Labs. Yang

Latest Version on Packagist Build Status Coverage Status Quality Score Total Downloads Gitter

Woohoo Labs. Yang is a PHP framework which helps you to communicate with JSON:API servers more easily.

Table of Contents

Introduction

JSON:API specification reached 1.0 on 29th May 2015 and we also believe it is a big day for RESTful APIs as this specification makes APIs more robust and future-proof than they have ever been. Woohoo Labs. Yang (named after Yin-Yang) was born to bring efficiency and elegance to your JSON:API clients, while Woohoo Labs. Yin is its server-side counterpart.

Features

Install

The only thing you need before getting started is Composer.

Install an HTTP client and message implementations:

Because Yang requires a HTTP client implementation, you must install one first. You may use Guzzle 7 Adapter or any other library of your preference:

Install Yang:

To install the latest version of this library, run the command below:

Note: The tests and examples won't be downloaded by default. You have to use composer require woohoolabs/yang --prefer-source or clone the repository if you need them.

Yang requires PHP 7.4 at least. You may use Yang 2.3 for PHP 7.2.

Basic Usage

Yang can help you in three ways to communicate with JSON:API servers. The following subsections will cover these topics.

Request builder

Yang comes with a powerful request builder with which you are able to setup PSR-7 Request objects in a JSON:API compliant way. For this purpose, you may use the JsonApiRequestBuilder class as it can be seen in the following example.

If you do not want to use the built-in Request Builder, you can freely setup any PSR-7 RequestInterface instances in order to proceed with the next steps:

HTTP clients

The library comes with support for PSR-18 and HTTPlug, so you can choose how you want to send your requests. If you installed the php-http/guzzle6-adapter package, then you will be able to use Guzzle to do so:

Of course, you can use any available HTTP Clients or create a custom HTTP Client thanks to PSR-18 and HTTPlug.

Response

As soon as you have retrieved the server response, you can start querying it. Yang uses the PSR-7 compatible JsonApiResponse class for this purpose. If you used a HTTP client introduced above, you will automatically get an object of this type, otherwise you have to take care of instantiating it with the right dependencies:

The JsonApiResponse class - above the ones defined by PSR-7 - has some methods to make the handling of JSON:API responses easier:

The Document class has various methods too:

The DocumentLinks class features the following methods:

The Error class has the following methods:

The ResourceObject class has the following methods:

The Relationship object supports the following methods:

Hydration

JSON:API responses with many related resources are not easily to process with the above approach. For example, if you want to retrieve the value of an attribute of a related resource, you need the following code:

This is a bit too much code to write, and it gets a lot worse when you want to map complex response documents with many relationships to objects:

This is the situation when using a hydrator can help you. Currently, Yang has only one hydrator, the ClassDocumentHydrator which - if the response was successful - maps the specified document to an stdClass along with all the resource attributes and relationships. It means that errors, links, meta data won't be present in the returned object. However, relationships are very easy to access now.

Let's use the document from the last example for demonstrating the power of hydrators:

That's all you need to do in order to create the same $dog object as in the first example! Now, you can display its properties:

Note: The method ClassDocumentHydrator::hydrateSingleResource() throws DocumentException when the document doesn't have any primary data or if the primary data is a collection. Otherwise - when the primary data is a single resource - an stdObject along with all the attributes and relationships is returned.

Additionally, you may use the ClassHydrator::hydrateCollection() method for retrieving many dogs:

Note: The method ClassHydrator::hydrateCollection() throws DocumentException when the primary data is a single resource. Otherwise - when the primary data is a collection of resources - an array of stdObjects along with all the attributes and relationship is returned.

Furthermore, there is a hydrate() method available for you when you don't care if the primary data is a single resource or a collection of resources.

Note: The method ClassDocumentHydrator::hydrate() returns an empty array when the document doesn't have any primary data. It returns an array containing a single stdClass if the primary data is a single resource. Otherwise - when the primary data is a collection of resources - an array of stdObjects is returned.

Advanced Usage

Custom serialization

Sometimes you might need to be tricky to serialize the request body in a custom way. For example, if you dispatch a server request internally (within the original request), then you can send the request body as an array thanks to this feature - so you don't need to serialize at client-side and then deserialize at server-size. If you use Woohoo Labs. Yin and a custom deserializer at server-side, then this is an easy task to do.

At client-side, if you use Yang with the Request Builder, then you only have to pass a second constructor argument to it like below to take advantage of custom serialization:

You only have to make sure that your custom serializer implements the SerializerInterface.

Custom deserialization

Sometimes you might need to be tricky to deserialize a server response in a custom way. For example, if you dispatch a server request internally (within the original request), then you can receive the response body as an array thanks to this feature - so you don't need to serialize at server-side and then deserialize at client-size. If you use Woohoo Labs. Yin and a custom serializer at server-side, then this is an easy task to do.

At client-side, if you use Yang with the default HTTP Clients then you only have to pass a second constructor argument to them like below to take advantage of custom deserialization:

Otherwise pass your deserializer to the JsonApiResponse as its second argument like below:

You only have to make sure that your custom deserializer implements the DeserializerInterface.

Examples

Have a look at the examples directory for a really basic example.

Versioning

This library follows SemVer v2.0.0.

Change Log

Please see CHANGELOG for more information what has changed recently.

Testing

Woohoo Labs. Yang has a PHPUnit test suite. To run the tests, run the following command from the project folder:

Additionally, you may run docker-compose up or make test in order to execute the tests.

Contributing

Please see CONTRIBUTING for details.

Support

Please see SUPPORT for details.

Credits

License

The MIT License (MIT). Please see the License File for more information.


All versions of yang with dependencies

PHP Build Version
Package Version
Requires php Version ^7.4.0||^8.0.0
php-http/httplug Version ^2.0.0
psr/http-client Version ^1.0.0
psr/http-message-implementation Version ^1.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package woohoolabs/yang contains the following files

Loading the files please wait ....