Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce an alternative to the serializer #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\SerializerBuilder;

$loader = require_once __DIR__ . '/vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, "loadClass"));

$serializer = SerializerBuilder::create()
->build();

final class Car
{
/** @Type("string") */
private $model;

/** @Type("string") */
private $brand;

public function __construct($brand, $model)
Expand All @@ -28,4 +16,29 @@ public function __toString()
{
return $this->brand . ' ' . $this->model;
}

public function toJson()
{
return json_encode(get_object_vars($this));
}

public function toXml()
{
return <<<XML
<result>
<model><![CDATA[$this->model]]></model>
<brand><![CDATA[$this->brand]]></brand>
</result>
XML;
}

public static function fromArray(array $data): self
{
if (!isset($data['brand']) || !isset($data['model'])) {
$message = "Missing data. Could not instantiate a Car object";
throw new InvalidArgumentException($message);
}

return new self($data['brand'], $data['model']);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"require": {
"jms/serializer": "^3.4"
"jms/serializer": "^3.4",
"ext-json": "*"
},
"config": {
"platform": {
Expand Down
19 changes: 10 additions & 9 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
require_once __DIR__ . '/bootstrap.php';

//Serialize
$opelAstra = new Car('Opel', 'Astra');
$jsonData = $serializer->serialize($opelAstra, 'json');
$xmlData = $serializer->serialize($opelAstra, 'xml');
$car = new Car('Opel', 'Astra');

//Schrijf het resultaat naar een file
file_put_contents("xmldata.xml", $xmlData);
file_put_contents("jsondata.json", $jsonData);
file_put_contents("xmldata.xml", $car->toXml());
file_put_contents("jsondata.json", $car->toJson());

// Deserialize
$objectOne = $serializer->deserialize(file_get_contents('jsondata.json'), Car::class,'json');
$objectTwo = $serializer->deserialize(file_get_contents('xmldata.xml'), Car::class,'xml');
// Lees de bestande uit en creeer een object
// Eerst van uit het json bestand
$jsonData = file_get_contents('jsondata.json');
echo Car::fromArray((array) json_decode($jsonData));

echo $objectOne;
echo PHP_EOL;
echo $objectTwo;
//Daarna van uit het xml bestand
$xmlData = file_get_contents('xmldata.xml');
echo Car::fromArray((array) simplexml_load_string($xmlData));