1. Go to this page and download the library: Download fabricio872/api-modeller 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/ */
fabricio872 / api-modeller example snippets
use Fabricio872\ApiModeller\ClientAdapter\ClientInterface;
use GuzzleHttp\Client;
class GuzzleClient implements ClientInterface
{
private Client $client;
public function __construct()
{
$this->client = new Client();
}
public function request(string $method, string $endpoint, array $options): string
{
return $this->client->request($method, $endpoint, $options)->getBody()->getContents();
}
}
use Doctrine\Common\Annotations\AnnotationReader;
use Fabricio872\ApiModeller\ClientAdapter\Symfony;
use Symfony\Component\HttpClient\HttpClient;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class Modeller
{
/** @var \Fabricio872\ApiModeller\Modeller */
private static $modeller;
public static function get()
{
if (!isset(self::$modeller)) {
$reader = new AnnotationReader();
$client = new GuzzleClient(); // class we have created above
$loader = new FilesystemLoader();
$twig = new Environment($loader, [
'cache' => '/path/to/compilation_cache',
]);
self::$modeller = new \Fabricio872\ApiModeller\Modeller(
$reader,
$client,
$twig
);
}
return self::$modeller;
}
}
// src/ApiModels/Users.php
use Fabricio872\ApiModeller\Annotations\Resource;
/**
* @Resource(
* endpoint="{{api_url}}/api/users",
* method="GET",
* type="json",
* options={
* "headers"={
* "accept"= "application/json"
* }
* }
* )
*/
class Users
{
public $page;
public $per_page;
public $total;
public $total_pages;
public $data;
}
// src/ApiModels/Users.php
use Fabricio872\ApiModeller\Annotations\Resource;
use Fabricio872\ApiModeller\Annotations\Resources;
/**
* @Resources({
* "multiple"= @Resource(
* endpoint="{{api_url}}/api/users",
* method="GET",
* type="json",
* options={
* "headers"={
* "accept"= "application/json"
* }
* }
* ),
* "single"= @Resource(
* endpoint="{{api_url}}/api/users/{{id}}",
* method="GET",
* type="json",
* options={
* "headers"={
* "accept"= "application/json"
* }
* }
* ),
* })
*/
class Users
{
public $page;
public $per_page;
public $total;
public $total_pages;
public $data;
}
// src/Controller/SomeController.php
public function index()
{
var_dump(Modeller::get()->getData(
Repo::new(Users::class)
->setOptions([
"query" => [
"page" => 2
]
])
));
}
// src/Controller/SomeController.php
public function index(Modeller $modeller)
{
var_dump(Modeller::get()->getData(
Repo::new(Users::class)
->setParameters([
"id" => 2
])
->setIdentifier("single")
));
}
// src/ApiModels/Data.php
use Fabricio872\ApiModeller\Annotations\Resource;
use Fabricio872\ApiModeller\Annotations\ModelTitle;
/**
* @Resource(
* endpoint="{{api_url}}/api/data",
* method="GET",
* type="json",
* options={
* "headers"={
* "accept"= "application/json"
* }
* }
* )
* @ModelTitle("data")
*/
class Data
{
public $myData;
}