Skip to content

Instantly share code, notes, and snippets.

@nebkam nebkam/api_client.php
Last active Nov 1, 2019

Embed
What would you like to do?
<?php
class ApiClient
{
/**
* @return Project[]
*/
public function getProjects(): array
{
$projects = [];
$response = $this->client->get('https://some-awesome.api/projects');
$projectCollection = $this->serializer->deserialize($response->getBody()->getContent(), ProjectCollection::class, 'json');
if (!empty($collection->getResult()))
{
foreach ($collection->getResult() as $project)
{
if ($project->isOnSale())
{
$projects[] = $project;
}
}
}
return $projects;
}
public function addNewProject(Project $project)
{
$this->client->post('https://some-awesome.api/projects', [
'body' => $this->serializer->serialize($project, 'json')
]);
}
}
<?php
use Symfony\Component\Serializer\Annotation\SerializedName;
class Project
{
/**
* @SerializedName("Id")
* @var int
*/
private $internalId;
/**
* @SerializedName("Name")
* @var string
*/
private $title;
/**
* @SerializedName("Quality")
* @var float
*/
private $quality;
// getters and setters
}
<?php
use Symfony\Component\Serializer\Annotation\SerializedName;
class ProjectCollection
{
/**
* @SerializedName("Result")
* @var Project[]
*/
private $result;
// getter
/**
* @param Project[] $results
*/
public function setResult(array $result)
{
$this->result = $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.