Last active
February 13, 2020 20:32
-
-
Save nebkam/81de7b5460d99049e73f12037222cae9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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') | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Serializer\Annotation\SerializedName; | |
class ProjectCollection | |
{ | |
/** | |
* @SerializedName("Result") | |
* @var Project[] | |
*/ | |
private $result; | |
/** | |
* @return Project[]|null | |
*/ | |
public function getResult(): ?array | |
{ | |
return $this->result; | |
} | |
/** | |
* @param Project[] $results | |
*/ | |
public function setResult(array $result) | |
{ | |
$this->result = $result; | |
} | |
} |
Great! I'm glad it worked! I learned a lot through your use case, thanks!
I've updated the article to suggest symfony/serializer-pack
and mentioned you in the DocBlock parsing ;)
Thx I learned a lot as well. And appreciate the mention. I am going to use this further in my project.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, finally I got Project objects returned now it works like I expected. Gonna try to fix the mapping. I also changed the example to use the SerializerInterface type hint as you suggested. The default serializer-pack of the website-skeleton installs all (hidden) dependencies.