PHPackages                             incraigulous/rest-repositories - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. incraigulous/rest-repositories

ActiveLibrary[API Development](/categories/api)

incraigulous/rest-repositories
==============================

Fetch data from any rest API with a common interface.

2.3.4(4y ago)1738[2 PRs](https://github.com/incraigulous/rest-repositories/pulls)MITPHP

Since Mar 15Pushed 3y ago1 watchersCompare

[ Source](https://github.com/incraigulous/rest-repositories)[ Packagist](https://packagist.org/packages/incraigulous/rest-repositories)[ RSS](/packages/incraigulous-rest-repositories/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (5)Versions (27)Used By (0)

Rest Repositories
=================

[](#rest-repositories)

Fetch data from any rest API with a common interface. Api results are returned wrapped in easy-to-work-with, fluent collection and object wrappers.

### In a nutshell

[](#in-a-nutshell)

You should be able to fetch data from any webservice and then access it using the following API:

```
	$posts = PostsRepository::all()->first()->title;

```

or

```
	$post = PostsRepository::find(1)->author->image->url;

```

This keeps you from having to create a separate mental map for each web service you work with.

Repositories
------------

[](#repositories)

It's the job of the repository to abstract working with your webservice to a common interface. Extend `Incraigulous\RestRepositories\Single`, `Incraigulous\RestRepositories\Listing` or `Incraigulous\RestRepositories\Resource` for minimal setup.

### Resources

[](#resources)

A resource is for API resources with full read/write capability. The resource object has the following methods:

```
	public static function get($params = []);
	public static function update($id, $params = []);
	public static function all();
	public static function find($id);
	public static function create($payload);
	public static function delete($payload);

```

To create a new resource extend `Incraigulous\RestRepositories\Resource`:

For example:

```
    use Incraigulous\RestRepositories\Resource;
    class PostsResource extends Resource
    {
        static $resource = 'posts'
        public static function sdk() {
            return new YourSdk();
        }
    }
```

### Listings

[](#listings)

A listing is for API resources with full read only capability. The resource object has the following methods:

```
	public static function get($params = []);
	public static function all();
	public static function find($id);

```

To create a new listing extend `Incraigulous\RestRepositories\Listing`:

### Singles

[](#singles)

A single is for API resources for a single listing endpoint. The resource object has only the `get` method.:

```
	public static function get($params = []);

```

To create a new single extend `Incraigulous\RestRepositories\Single`:

### Creating a base repository for a webservice

[](#creating-a-base-repository-for-a-webservice)

```
	//This is just an example, you should create your own SDK implementation
	use Incraigulous\RestRepositories\Sdks\JsonPlaceholderSdk;

	use Incraigulous\RestRepositories\Listing;

	class JsonPlaceholderBaseRepository extends Listing
	{
	    public static function sdk() {
	        return new JsonPlaceholderSdk();
	    }
	}

```

### Creating a repository

[](#creating-a-repository)

```
	class JsonPlaceholderPostsRepository extends JsonPlaceholderBaseRepository
	{
	    //This will be passed to your SDK as the resource. This is usually the last part of the URL.
	    public static $resource = 'posts';

	    //In case you need to set default paramaters on all requests from repository.
	    protected static function defaultParams()
	    {
	        return [
	            'somefilter' => 'fromapi'
            ]
	    }
	}

	$posts = JsonPlaceholderPostsRepository::all();

```

### Getting the original data

[](#getting-the-original-data)

Responses are wrapped in Collection and Object wrappers by default and data keys are stripped out. To access response meta data like pagination or links, you can retrieve the original object like so:

```
    PostRepository::all()->getOriginal()['page'];
    //Or
    PostRepository::all()->first()->getOriginal()['links'];
    //Or
    PostRepository::all()->first()->author->getOriginal()['links'];

```

SDKs
----

[](#sdks)

It's the job of an SDK class to make requests to a webservice. SDKs should implement `Incraigulous\RestRepositories\Contracts\SdkInterface`. To make this easy, a base `Incraigulous\RestRepositories\Sdks\HttpSdk` class is provided.

### Using the HttpSdk class on the fly

[](#using-the-httpsdk-class-on-the-fly)

#### Without authentication

[](#without-authentication)

```
	$sdk = new HttpSdk('https://jsonplaceholder.typicode.com/');
	$result = $sdk->post('posts', ['title' => 'foo', 'body' => 'bar', 'userId' => 1]);

```

#### With authentication

[](#with-authentication)

```
	$sdk = new HttpSdk('http://example.com/api/', ['headers' => ['Authorization' => 'password123']]);
	$result = $sdk->post('posts', ['title' => 'foo', 'body' => 'bar', 'userId' => 1]);

```

### Even Better: Creating your own SDK class

[](#even-better-creating-your-own-sdk-class)

#### Without authentication

[](#without-authentication-1)

```
