dev-master
9999999-dev http://www.github.com/linkorb/primatePrimate: Restful API Library
MIT
The Requires
- php >=5.3.0
The Development Requires
api php json restful linkorb primate
Wallogit.com
2017 © Pedro Peláez
Primate: Restful API Library
, (*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)
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)
In Primate APIs, a client can work with Resources and Collections., (*7)
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)
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 resourceAdditionally, you can pass an array of expands., (*16)
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"
}
}
MIT (see LICENSE.md), (*20)

Check out our other projects at linkorb.com/engineering., (*21)
Btw, we're hiring!, (*22)
Primate: Restful API Library
MIT
api php json restful linkorb primate