2017 © Pedro Peláez
 

library api-transformer

Base library providing the core functionality for API transformation.

image

nilportugues/api-transformer

Base library providing the core functionality for API transformation.

  • Friday, April 7, 2017
  • by nilportugues
  • Repository
  • 4 Watchers
  • 8 Stars
  • 49,228 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 9 Forks
  • 4 Open issues
  • 35 Versions
  • 4 % Grown

The README.md

API Transformer

Build Status Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Total Downloads License Donate, (*1)

Purpose

This library provides the core functionality for API transformation and is a base library for many other packages., (*2)

By itself it's not usable at all. Check the projects using it below., (*3)

Used by

Currently the following transformers make use of this library as foundation:, (*4)

Installation

Use Composer to install the package:, (*5)

$ composer require nilportugues/api-transformer

How it works

Loading must be done using the Mapper class, as it expects an array containing a defined structure, or a class name implementing ApiMapping., (*6)

There are 2 styles for flexibility when integrating the library into PHP frameworks., (*7)

While I discourage having 2 styles for mapping it is well possible to have them side by side in the very same configuration file. The Mapper class that loads all Mappings does internal transformation for both, so client does not have to worry., (*8)

Usage

use NilPortugues\Api\Mapping\Mapper;
use NilPortugues\AcmeProject\Infrastructure\Api\Mappings\PostApiMapping;

$arrayConfig = include 'mappings.php';
$classConfig = [
    PostApiMapping::class,
];

$mappings = array_merge($classConfig, $arrayConfig);

//Now $mapper can be passed to a Transformer.
$mapper = new Mapper($mappings);


, (*9)

Creating the Mapping files

Implementing ApiMapping (Prefered method)

To create a Mapping you may implement the following interfaces:, (*10)

  • ApiMapping: transform your data into plain JSON for API consumtion or the JSend API format.
  • JsonApiMapping to transform your data into the JSONAPI 1.0 standard.
  • HalMapping to transform your data to HAL+JSON and HAL+XML API standards.

As expected you may implement many interfaces to support multiple API formats., (*11)

<?php

namespace NilPortugues\AcmeProject\Infrastructure\Api\Mappings;

use NilPortugues\AcmeProject\Blog\Domain\Post;
use NilPortugues\Api\Mappings\HalMapping;
use NilPortugues\Api\Mappings\JsonApiMapping;

class PostApiMapping implements JsonApiMapping, HalMapping
{
    /**
     * {@inheritdoc}
     */
    public function getClass() : string
    {
        return Post::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getAlias() : string
    {
        return 'Posting'; //If none is used 'Post' will be used instead.
    }

    /**
     * {@inheritdoc}
     */
    public function getAliasedProperties() : array
    {
        return [
            'title' => 'headline',
            'content' => 'body',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getHideProperties() : array
    {
        return [
           'comments',
       ];
    }

    /**
     * {@inheritdoc}
     */
    public function getIdProperties() : array
    {
        return [
            'postId',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getUrls() : array
    {
        return [
          // Mandatory
          'self' => 'http://example.com/posts/{postId}',
          // Optional
          'comments' => 'http://example.com/posts/{postId}/comments',
        ];
    }

    /**
     * Returns an array of curies.
     *
     * @return array
     */
    public function getCuries() : array
    {
        return [
            'name' => 'example',
            'href' => 'http://example.com/docs/rels/{rel}',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getRelationships() : array
    {
        return [
            'author' => [
                'related' => 'http://example.com/posts/{postId}/author',
                'self' => 'http://example.com/posts/{postId}/relationships/author',
            ],
        ];
    }
}

Mapping using an array

// mappings.php

return  [
  [
      'class' => Post::class,
      'alias' => 'Posting', //If none is used 'Post' will be used instead.
      'aliased_properties' => [
          'title' => 'headline',
          'content' => 'body',
      ],
      'hide_properties' => [
          'comments',
      ],
      'id_properties' => [
          'postId',
      ],
      'urls' => [
          // Mandatory
          'self' => 'http://example.com/posts/{postId}',
          // Optional
          'comments' => 'http://example.com/posts/{postId}/comments',
      ],
      // (Optional) Used by HAL+JSON / HAL+XML
      'curies' => [
          'name' => 'example',
          'href' => 'http://example.com/docs/rels/{rel}',
      ],
      // (Optional) Used by JSONAPI
      'relationships' => [
          'author' => [
            'related' => 'http://example.com/posts/{postId}/author',
            'self' => 'http://example.com/posts/{postId}/relationships/author',
          ],
      ]
  ],
];

Quality

To run the PHPUnit tests at the command line, go to the tests directory and issue phpunit., (*12)

This library attempts to comply with PSR-1, PSR-2, PSR-4 and PSR-7., (*13)

If you notice compliance oversights, please send a patch via Pull Request., (*14)

Contribute

Contributions to the package are always welcome!, (*15)

Support

Get in touch with me using one of the following means:, (*16)

Authors

License

The code base is licensed under the MIT license., (*17)

The Versions

07/04 2017

dev-master

9999999-dev http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

07/04 2017

3.1.1

3.1.1.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

13/03 2017

3.1.0

3.1.0.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

11/08 2016
02/06 2016

1.6.0

1.6.0.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

02/06 2016

2.0.0

2.0.0.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

20/10 2015

1.2.1

1.2.1.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

20/10 2015

1.2.0

1.2.0.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

20/10 2015

1.1.3

1.1.3.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

16/10 2015

1.1.2

1.1.2.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

16/10 2015

1.1.1

1.1.1.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

16/10 2015

1.1.0

1.1.0.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

14/10 2015

1.0.8

1.0.8.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

14/10 2015

1.0.7

1.0.7.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

02/10 2015

1.0.6

1.0.6.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

28/08 2015

1.0.5

1.0.5.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

22/08 2015

1.0.4

1.0.4.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

22/08 2015

1.0.3

1.0.3.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

16/08 2015

1.0.2

1.0.2.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

16/08 2015

1.0.1

1.0.1.0 http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

15/08 2015
14/08 2015

1.0.1-alpha

1.0.1.0-alpha http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer

14/08 2015

1.0.0-alpha

1.0.0.0-alpha http://nilportugues.com

Base library providing the core functionality for API transformation.

  Sources   Download

MIT

The Requires

 

The Development Requires

api serializer psr7 response transformer