Skip to content

Instantly share code, notes, and snippets.

@parijke
Last active February 12, 2020 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parijke/05ac5d4332c1633ef23be25027a24965 to your computer and use it in GitHub Desktop.
Save parijke/05ac5d4332c1633ef23be25027a24965 to your computer and use it in GitHub Desktop.
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.2.5",
"ext-ctype": "*",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.1",
"symfony/asset": "5.0.*",
"symfony/console": "5.0.*",
"symfony/dotenv": "5.0.*",
"symfony/expression-language": "5.0.*",
"symfony/flex": "^1.3.1",
"symfony/form": "5.0.*",
"symfony/framework-bundle": "5.0.*",
"symfony/http-client": "5.0.*",
"symfony/intl": "5.0.*",
"symfony/mailer": "5.0.*",
"symfony/monolog-bundle": "^3.1",
"symfony/notifier": "5.0.*",
"symfony/orm-pack": "*",
"symfony/process": "5.0.*",
"symfony/property-info": "5.0.*",
"symfony/security-bundle": "5.0.*",
"symfony/serializer-pack": "^1.0",
"symfony/string": "5.0.*",
"symfony/translation": "5.0.*",
"symfony/twig-pack": "*",
"symfony/validator": "5.0.*",
"symfony/web-link": "5.0.*",
"symfony/yaml": "5.0.*"
},
"require-dev": {
"symfony/debug-pack": "*",
"symfony/maker-bundle": "^1.0",
"symfony/profiler-pack": "*",
"symfony/test-pack": "*"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "5.0.*"
}
}
}
{
"success": true,
"result": [
{
"Name": "Project A",
"isonsale": "1"
},
{
"Name": "Project B",
"isonsale": "1"
},
{
"Name": "Project C",
"isonsale": "0"
}
]
}
<?php
namespace App\Model;
use Symfony\Component\Serializer\Annotation\SerializedName;
class Project {
/**
* @SerializedName("Name")
* @var string
*/
private $title;
/**
* @SerializedName("isonsale")
* @var string
*/
private $sale;
/**
* @param string $title
*/
public function setTitle(string $title): void
{
$this->title = $title;
}
/**
* @param string $sale
*/
public function setSale(string $sale): void
{
$this->sale = $sale;
}
}
<?php
namespace App\Model;
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;
}
}
<?php
namespace App\Controller;
use App\Model\ProjectCollection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;
class TestDeserializeController extends AbstractController {
/**
* @Route("/test/deserialize", name="test_deserialize")
*/
public function index(SerializerInterface $serializer) {
$projects = [];
$client = HttpClient::create();
$response = $client->request('GET', 'https://gist.github.com/parijke/05ac5d4332c1633ef23be25027a24965/raw/638941a29c0aa4c4d271e29b142e0472f88496bd/data');
$collection = $serializer->deserialize($response->getContent(), ProjectCollection::class, 'json');
if (!empty($collection->getResult()))
{
foreach ($collection->getResult() as $project)
{
$projects[] = $project;
}
}
dd($projects);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment