This model provides an eloquent-like base class that can be used to build custom models for remote APIs.
Install using composer:
$ composer require gruz/remote-model
To implement a custom API request method in the model, simple extend the Gruz\RemoteModel\Model class and use that extended model in the app models.
Example
<?phpnamespaceApp;
useAPIClient;
useGruz\RemoteModel\Model;
class BaseModel extends Model
{
/** * Make request through API. * * @return mixed */protectedfunctionrequest($endpoint = null, $method, $params)
{
$endpoint = $endpoint ? $endpoint : $this->endpoint;
$results = APIClient::$method($endpoint, $params);
return$results ? $this->newInstance($results) : null;
}
}$client = new Client();
$client->{ENDPOINT}()->{METHOD}();
ENDPOINT
The "snake case", plural name of the model class will be used as the endpoint name unless another name is explicitly specified. Using protected $endpoint = 'users'; at the top of the model, this is similar to the $table variable in Laravel models.
METHOD This is the action to take on the endpoint. It can be anything that the wrapper class provides.
<?phpnamespacePackageName\Api;
usePackageName\Api\Exception\BadMethodCallException;
usePackageName\Api\Exception\InvalidArgumentException;
class Client
{
/** * The HTTP client instance used to communicate with API. * * @var HttpClient */private$httpClient;
/** * Instantiate a new client. */publicfunction__construct()
{
$this->httpClient = newHttpClient;
}
/** * @param string $name * * @throws InvalidArgumentException * * @return ApiInterface */publicfunctionapi($name)
{
switch ($name)
{
case'users':
$api = newEndpoints\Users($this);
break;
case'reviews':
$api = newEndpoints\Reviews($this);
break;
default:
thrownewInvalidArgumentException(sprintf('Undefined api instance called: "%s"', $name));
}
return$api;
}
/** * @param string $name * * @throws InvalidArgumentException * * @return ApiInterface */publicfunction__call($name, $args)
{
try {
return$this->api($name);
}
catch (InvalidArgumentException$e) {
thrownewBadMethodCallException(sprintf('Undefined method called: "%s"', $name));
}
}
}This is just to give an example.
<?phpnamespacePackageName\Api\Endpoints;
usePackageName\Api\Client;
class Users
{
/** * The client. * * @var \PackageName\Api\Client */protected$client;
/** * @param \PackageName\Api\Client $client */publicfunction__construct(Client$client)
{
$this->client = $client;
}
/** * Register a user. * * @param array $params * * @return array */publicfunctionadd(array$params)
{
return$this->client->post('users', $params);
}
/* * Update user data * * @param array $params * * @return object */publicfunctionupdate(array$params)
{
return$this->client->patch('users/self', $params);
}
/** * Get extended information about a user by its id. * * @param string $user_id * * @return array */publicfunctionfind($user_id)
{
return$this->client->get('users/'.rawurlencode($user_id));
}
}An API client must be set before any data can be retrieved . To set the client use the static Model::setClient method.
Below is an example of the service provider way of setting the client.
<?phpnamespaceApp\Providers;
useGruz\RemoteModel\Model;
usePackageName\API\Client;
useIlluminate\Support\ServiceProvider;
class ApiServiceProvider extends ServiceProvider
{
/** * Bootstrap the application events. * * @return void */publicfunctionboot()
{
Model::setClient($this->app['apiclient']);
}
/** * Register the service provider. * * @return void */publicfunctionregister()
{
$this->app->singleton('apiclient', function () {
returnnewClient(); // API Client
});
}
/** * Get the services provided by the provider. * * @return string[] */publicfunctionprovides()
{
return [
'apiclient'
];
}
}<?phpnamespaceApp;
useDateTime;
useGruz\RemoteModel\ModelasBaseModel;
class User extends BaseModel
{
protected$hidden = [
'password'
];
protected$casts = [
'age' => 'integer'
];
publicfunctionsave()
{
returnAPI::post('/items', $this->attributes);
}
publicfunctionsetBirthdayAttribute($value)
{
$this->attributes['birthday'] = strtotime($value);
}
publicfunctiongetBirthdayAttribute($value)
{
returnnewDateTime("@$value");
}
publicfunctiongetAgeAttribute($value)
{
return$this->birthday->diff(newDateTime('now'))->y;
}
}Using model
$item = newUser([
'name' => 'john'
]);
$item->password = 'bar';
echo$item; // {"name":"john"}