2017 © Pedro Peláez
 

library primate

Primate: Restful API Library

image

linkorb/primate

Primate: Restful API Library

  • Saturday, April 30, 2016
  • by joostfaassen
  • Repository
  • 3 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 6 % Grown

The README.md

Primate: The API ToolKit

, (*1)

Primate is a library that helps creating beautiful REST APIs., (*2)

It is heavily inspired by Stormpath's "The Fundamentals of REST API Design", (*3)

Using Primate

First you'll need to instantiate Primate, before your app can serve requests:, (*4)

use Primate\Primate;

$primate = new Primate();
$primate->setBaseUrl('http://primate.example.com/api/v1');
$primate->setProperty('tenant', 'joe');
$primate->setProperty('x', 'y');

You'll notice that the BaseUrl of your API is set on the Primate instance, in order to output correct urls., (*5)

Additionally, we're registering some arbitrary properties to define the context of the requests. You can use these properties later in order to fetch resources., (*6)

Resources and Collections

In Primate APIs, a client can work with Resources and Collections., (*7)

  • A Resource is simply an "object" in your application. For example, a User, a Product, etc.
  • A Collection is simply an array of Resources.

Types

Each Resource is of a specified Type., (*8)

In Primate, you'll need to register one or more Types before you can use them. For example:, (*9)

$repo = new MyContactRepository();
$type = new Type('contacts', $repo);
$primate->registerType($type);

Each Type has a name and a repository. The Repository can be any class that's implementing the Primate\RepositoryInterface., (*10)

It's recommended to take your existing application repositories, and make them implement this interface., (*11)

For example:, (*12)

<?php

namespace Example;

use Primate\RepositoryInterface;
use Primate\Resource;
use Primate\Primate;
use RuntimeException;

class PdoContactRepository implements RepositoryInterface
{
    public function __construct($pdo)
    {
        $this->pdo = $pdo;
    }

    // Implement your regular repository methods here...

    public function loadResourceCollection(Collection $collection, Primate $primate)
    {
        foreach ($this->getAllContacts() as $contact) {
            $resource = new Resource($collection->getType(), $contact->getId());
            $collection->addResource($resource);
        }
    }

    public function loadResources($resources, Primate $primate)
    {
        foreach ($resources as $resource) {
            $contact = $this->findById($resource->getId());
            $resource->setProperty('name', $contact->getName());
            $resource->setProperty('gender', $contact->getGender());
            $phoneResource = $primate->createResource('phones', $contact->getPhoneId());
            $resource->setProperty('phone', $phoneResource);
        }
    }
}

Primate will call these methods to load resource data., (*13)

Calling Primate

You can make requests to Primate like this:, (*14)

$data = $primate->getDataByPath($path, $expands);
echo json_encode($data, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);

The path variable is either:, (*15)

  • /{typeName}: This will return a collection of all resources of that type
  • /{typeName}/{resourceId}: This will return the specific resource

Additionally, you can pass an array of expands., (*16)

Sub-resource expansion

When passing keys in the expands parameter, Primate will automatically expand the specified sub-resources., (*17)

For example, if your first response looks like this:, (*18)

{
    "href": "http://primate.example.com/api/v1/contacts/1",
    "name": "Alice",
    "gender": "Female",
    "phone": {
        "href": "http://primate.example.com/api/v1/phones/xyz"
    }
}

You can ask Primate to 'expand the phone property:, (*19)

{
    "href": "http://primate.example.com/api/v1/contacts/1",
    "name": "Alice",
    "gender": "Female",
    "phone": {
        "href": "http://primate.example.com/api/v1/phones/xyz",
        "number": "+1 987654321",
        "type": "mobile"
    }
}

License

MIT (see LICENSE.md), (*20)

Brought to you by the LinkORB Engineering team


Check out our other projects at linkorb.com/engineering., (*21)

Btw, we're hiring!, (*22)

The Versions

30/04 2016

dev-master

9999999-dev http://www.github.com/linkorb/primate

Primate: Restful API Library

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

api php json restful linkorb primate