1. Go to this page and download the library: Download codecasts/restinga library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codecasts / restinga example snippets
namespace Codecasts\DigitalOcean;
use Codecasts\Restinga\Authorization\Bearer;
use Codecasts\Restinga\Service\Descriptor;
class DigitalOceanDescriptor extends Descriptor
{
// the identifier of the service that
// we're gonna use later when creating
// our resource classes
protected $service = 'digital-ocean';
// the prefix to be used when calling the api
protected $prefix = 'https://api.digitalocean.com/v2';
// this method returns an Authorization class instance
// There are a few available, just check
// the Codecasts\Restinga\Authorization namespace
public function authorization()
{
return new Bearer('your-token-here');
}
}
$digital_ocean = new Codecasts\DigitalOcean\DigitalOceanDescriptor();
Codecasts\Restinga\Container::register($digital_ocean);
namespace Codecasts\DigitalOcean\Resource;
use Codecasts\Restinga\Data\Resource;
use Codecasts\Restinga\Http\Format\Receive\ReceiveJson;
use Codecasts\Restinga\Http\Format\Receive\ReceiveJsonErrors;
use Codecasts\Restinga\Http\Format\Send\SendJson;
class Droplet extends Resource
{
// Each resource will need to use 2 traits to
// define in which format it should send & receive data.
// In your example, Digital Ocean uses Json to exchange data.
use ReceiveJson;
use SendJson;
// Also, a format for errors are needed
use ReceiveJsonErrors;
// the identifier of which service restinga should use when
// handling this resource
// the name should match with the one you defined on the Service Descriptor
protected $service = 'digital-ocean';
// In this attribute, we dine the resource name, that will be used
// as the sufix for the already defined api prefix
protected $name = 'droplets';
// The identifier is the main attribute that should be used when calling the api for
// the current resource
protected $identifier = 'id';
// When receiving Data, the result may be nested inside
// a response object, like {"items": {"first_item"}}
// this attribute is where you set the root element for the response
// when searching for a collection (multiple items)
protected $collection_root = 'droplets';
// works like $collection_root, but now this attribute
// sets the root when a single result is expected
// (like getting a droplet by using an id)
protected $item_root = 'droplet';
}
use Codecasts\DigitalOcean\Resource\Droplet;
$droplet = new Droplet();
$droplet->name = 'server.restinga.dev';
$droplet->region = 'nyc3';
$droplet->size = '512mb';
$droplet->image = 'ubuntu-14-04-x64';
$saved = $droplet->save();
if ($saved) {
echo $droplet->id; // 4242424
}
use Codecasts\DigitalOcean\Resource\Droplet;
$droplet = new Droplet();
$droplet->name = 'server.restinga.dev';
$droplet->region = 'nyc3';
$droplet->size = '512mb';
// Image is d an invalid image for Droplet creation."
echo implode(', ', $droplet->getErrors()->all());
}
use Codecasts\DigitalOcean\Resource\Droplet;
$droplet = new Droplet();
$found = $droplet->find('4242424'); // the id of the droplet we just created
if ($found) {
echo $droplet->name; // server.restinga.dev
} else {
foreach ($droplet->errors->all() as $code => $error) {
echo $code . ": " . $error . "\n";
}
// id: not_found
// message: The resource you were accessing could not be found.
}
use YourApp\Resources\YourResource();
$resource = new YourResource();
$resource->find('123');
$resource->price = '43.99';
$resource->update();
use YourApp\Resources\YourResource();
$resource = new YourResource();
$resource->find('123');
$resource->destroy();
namespace Codecasts\DigitalOcean\Resource;
use Codecasts\Restinga\Data\Resource;
use Codecasts\Restinga\Http\Format\Receive\ReceiveJson;
use Codecasts\Restinga\Http\Format\Receive\ReceiveJsonErrors;
use Codecasts\Restinga\Http\Format\Send\SendJson;
class Domain extends Resource
{
use ReceiveJson;
use SendJson;
use ReceiveJsonErrors;
protected $service = 'digital-ocean';
protected $name = 'domains';
protected $identifier = 'name';
protected $collection_root = 'domains';
protected $item_root = 'domain';
}
namespace Codecasts\DigitalOcean\Resource;
use Codecasts\Restinga\Data\Resource;
use Codecasts\Restinga\Http\Format\Receive\ReceiveJson;
use Codecasts\Restinga\Http\Format\Receive\ReceiveJsonErrors;
use Codecasts\Restinga\Http\Format\Send\SendJson;
class DomainRecord extends Resource
{
use ReceiveJson;
use SendJson;
use ReceiveJsonErrors;
protected $service = 'digital-ocean';
protected $name = 'records';
protected $identifier = 'id';
protected $collection_root = 'records';
protected $item_root = 'record';
}
public function record()
{
return $this->childResource(new DomainRecord());
}