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

Demo configuration #3

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
68 changes: 66 additions & 2 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Naming\IdenticalPropertyNamingStrategy;
use JMS\Serializer\SerializerBuilder;

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

$serializer = SerializerBuilder::create()
->addMetadataDir(__DIR__ . '/configuration')
->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy())
->build();

final class Car
Expand All @@ -17,15 +21,75 @@ final class Car

/** @Type("string") */
private $brand;
/**
* @var Owner
*/
private $owner;
/**
* @var ArrayCollection
*/
private $previousOwners;

public function __construct($brand, $model)
public function __construct($brand, $model, Owner $owner, ArrayCollection $ownerCollection)
{
$this->brand = $brand;
$this->model = $model;
$this->owner = $owner;
$this->previousOwners = $ownerCollection;
}

public function __toString()
{
return $this->brand . ' ' . $this->model;
$previousOwnerCount = count($this->previousOwners);
$ending = 'owners';

if ($previousOwnerCount === 1) {
$ending = 'owner';
}

$first = sprintf("This %s %s belongs to %s and it had %d previous %s",
$this->brand,
$this->model,
$this->owner,
count($this->previousOwners),
$ending
);

$second = '<br />';
$second.= 'the previous owners where: ' . '<br />';
/** @var Owner $owner */
foreach ($this->previousOwners as $owner) {
$second .= $owner->getOwner() . '<br />';
}

return $first . $second;
}
}


final class Owner
{
/** @var string */
private $name;

/**
* @return string
*/
public function getOwner(): string
{
return $this->name;
}

/**
* @param string $name
*/
public function updateOwner(string $name): void
{
$this->name = ucwords($name);
}

public function __toString()
{
return $this->getOwner();
}
}
18 changes: 18 additions & 0 deletions car.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"brand":"opel",
"model_name": "astra",
"currentOwner": {
"name":"Some Dude"
},
"previousOwners": [
{
"name": "barbara bush"
},
{
"name": "koni chiwa"
},
{
"name": "baka baka"
}
]
}
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"require": {
"jms/serializer": "^3.4"
},
"config": {
"platform": {
"php": "7.3.*"
}
"jms/serializer": "^3.4",
"doctrine/collections": "^1.6",
"symfony/yaml": "^5.0"
}
}
189 changes: 188 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions configuration/Car.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Car:
properties:
brand:
type: "string"
serialized_name: brand
model:
type: "string"
serialized_name: "model_name"
owner:
type: Owner
serialized_name: currentOwner
previousOwners:
type: ArrayCollection <Owner>
7 changes: 7 additions & 0 deletions configuration/Owner.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Owner:
properties:
name:
type: string
serialized_name: name
accessor:
setter: updateOwner
20 changes: 4 additions & 16 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@
/** @noinspection ALL */
require_once __DIR__ . '/bootstrap.php';

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

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

// Deserialize
$objectOne = $serializer->deserialize(file_get_contents('jsondata.json'), Car::class,'json');
$objectTwo = $serializer->deserialize(file_get_contents('xmldata.xml'), Car::class,'xml');

echo $objectOne;
echo PHP_EOL;
echo $objectTwo;
//Deserialize
$jsonData = file_get_contents(__DIR__ . '/car.json');
$car = $serializer->deserialize($jsonData, Car::class, 'json');
echo $car;