2017 © Pedro Peláez
 

library doctrine-rest

Rest API library for Doctrine 2 ORM

image

pz/doctrine-rest

Rest API library for Doctrine 2 ORM

  • Wednesday, July 11, 2018
  • by R3VoLuT1OneR
  • Repository
  • 1 Watchers
  • 4 Stars
  • 313 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 57 % Grown

The README.md

doctrine-rest

Build Status Scrutinizer Code Quality Scrutinizer Code Coverage, (*1)

Framework agnostic, library provides basic tools for implementation of JSON API over Doctrine library, (*2)

Using by default symfony/http-foundation for requests/responses and league/fractal for Rest response build., (*3)

Install

Add composer package to your project, (*4)

composer require pz/doctrine-rest

Usage

Package provides different actions for data manipulation and formatting., (*5)

Create entity and fractal trasformer for the entity., (*6)

// Entity class to work with
$entityClass = 'User';
$entityTransformer = new EntityTransformer();

If you want to use JSON API please implement JsonApiResource on your doctrine entity and add next header to request:, (*7)

Accept: application/vnd.api+json

Change entity repository to RestRepository or create new one., (*8)

// Provide configured entity manager
$entityManager = getEntityManager()

// Repository that action will work with
$restRepository = new RestRepository($entityManager, $entityManager->getClassMetadata($entityClass));

Prepare RestRequest entity or implement RestRequestContract on your custom RestRequest implementation., (*9)

// Get http request from framework or init yourself
$httpRequest = Symfony\Component\HttpFoundation\Request::createFromGlobals();
$restRequest = new RestRequest($httpRequest);

Collection (Index) action

Route request GET http://localhost/api/{resourceKey}, (*10)

$action = new CollectionAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response, (*11)

{
    'data': [
        { ...transformer data },
        { ...transformer data },
        { ...transformer data },
    ],
    'meta': [
        'pagination': { ... paginator data },
    ]
}

Json api response, (*12)

{
    'data': [
        {
            'id': {entityId},
            'type': {etntityResourceKey},
            'attributes': { ...transformer data },
            'relationships': { ..transformer includes },
            'links': {
                'self': 'http://localhost/api/resourceKey/{entityId}
            }
        },
        ... Other entities
    ],
    'meta': [
        'pagination': { ... paginator data },
    ]
}

Item (Get) action

Route request GET http://localhost/api/{resourceKey}/{id}., (*13)

$action = new ItemAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response, (*14)

{
    'data': [
        'id': {id},
         { ...transformer data }
    ],
}

Json api response, (*15)

{
    'data': {
        'id': {entityId},
        'type': {etntityResourceKey},
        'attributes': { ...transformer data },
        'relationships': { ..transformer includes },
        'links': {
            'self': 'http://localhost/api/resourceKey/{entityId}
        }
    },
}

Create action

Route request POST http://localhost/api/{resourceKey}., (*16)

$action = new CreateAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response, (*17)

{
    'data': [
        'id': {id},
         { ...transformer data }
    ],
}

Json api response, (*18)

{
    'data': {
        'id': {entityId},
        'type': {etntityResourceKey},
        'attributes': { ...transformer data },
        'relationships': { ..transformer includes },
        'links': {
            'self': 'http://localhost/api/resourceKey/{entityId}
        }
    },
}

Update action

Route request PATCH http://localhost/api/{resourceKey}/{id}., (*19)

$action = new UpdateAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Regular response, (*20)

{
    'data': [
        'id': {id},
         { ...transformer data }
    ],
}

Json api response, (*21)

{
    'data': {
        'id': {entityId},
        'type': {etntityResourceKey},
        'attributes': { ...transformer data },
        'relationships': { ..transformer includes },
        'links': {
            'self': 'http://localhost/api/resourceKey/{entityId}
        }
    },
}

Delete action

Route request DELETE http://localhost/api/{resourceKey}/{id}., (*22)

$action = new DeleteAction($restRepository, $entityTransformer);

/** @var RestResponse|Symfony\Component\HttpFoundation\Response */
$response = $action->dispatch($restRequest);

Response, (*23)

HTTP STATUS 204 NO CONTENT

Development

Generate doctrine migration diff

We using doctrine migrations for unit tests database schema., (*24)

php ./vendor/bin/doctrine-migrations migrations:diff

Run tests

docker compose run php phpunit

The Versions