From 753edf9447b0b4178bd7242a92e0ff55a9a96d42 Mon Sep 17 00:00:00 2001 From: peeperklip Date: Thu, 16 Jan 2020 11:41:05 +0100 Subject: [PATCH] Demo configuration --- bootstrap.php | 68 +++++++++++++- car.json | 18 ++++ composer.json | 9 +- composer.lock | 189 ++++++++++++++++++++++++++++++++++++++- configuration/Car.yaml | 13 +++ configuration/Owner.yaml | 7 ++ index.php | 20 +---- 7 files changed, 299 insertions(+), 25 deletions(-) create mode 100644 car.json create mode 100644 configuration/Car.yaml create mode 100644 configuration/Owner.yaml diff --git a/bootstrap.php b/bootstrap.php index d474d97..7ef4931 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -1,13 +1,17 @@ addMetadataDir(__DIR__ . '/configuration') + ->setPropertyNamingStrategy(new IdenticalPropertyNamingStrategy()) ->build(); final class Car @@ -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 = '
'; + $second.= 'the previous owners where: ' . '
'; + /** @var Owner $owner */ + foreach ($this->previousOwners as $owner) { + $second .= $owner->getOwner() . '
'; + } + + 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(); } } diff --git a/car.json b/car.json new file mode 100644 index 0000000..4e7c26b --- /dev/null +++ b/car.json @@ -0,0 +1,18 @@ +{ + "brand":"opel", + "model_name": "astra", + "currentOwner": { + "name":"Some Dude" + }, + "previousOwners": [ + { + "name": "barbara bush" + }, + { + "name": "koni chiwa" + }, + { + "name": "baka baka" + } + ] +} diff --git a/composer.json b/composer.json index 600c9d3..c1cb876 100644 --- a/composer.json +++ b/composer.json @@ -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" } } diff --git a/composer.lock b/composer.lock index f54a1a4..f2c68b1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ba11221f2020ee63bc7b6a4e0425cd52", + "content-hash": "d8c296fad8af2ef82c4565cd4ef18bce", "packages": [ { "name": "doctrine/annotations", @@ -74,6 +74,76 @@ ], "time": "2019-10-01T18:55:10+00:00" }, + { + "name": "doctrine/collections", + "version": "1.6.4", + "source": { + "type": "git", + "url": "https://github.com/doctrine/collections.git", + "reference": "6b1e4b2b66f6d6e49983cebfe23a21b7ccc5b0d7" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/collections/zipball/6b1e4b2b66f6d6e49983cebfe23a21b7ccc5b0d7", + "reference": "6b1e4b2b66f6d6e49983cebfe23a21b7ccc5b0d7", + "shasum": "" + }, + "require": { + "php": "^7.1.3" + }, + "require-dev": { + "doctrine/coding-standard": "^6.0", + "phpstan/phpstan-shim": "^0.9.2", + "phpunit/phpunit": "^7.0", + "vimeo/psalm": "^3.2.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.6.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\Collections\\": "lib/Doctrine/Common/Collections" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.", + "homepage": "https://www.doctrine-project.org/projects/collections.html", + "keywords": [ + "array", + "collections", + "iterators", + "php" + ], + "time": "2019-11-13T13:07:11+00:00" + }, { "name": "doctrine/instantiator", "version": "1.3.0", @@ -1116,6 +1186,123 @@ "xml" ], "time": "2019-12-14T20:49:23+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.13.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/f8f0b461be3385e56d6de3dbb5a0df24c0c275e3", + "reference": "f8f0b461be3385e56d6de3dbb5a0df24c0c275e3", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.13-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "time": "2019-11-27T13:56:44+00:00" + }, + { + "name": "symfony/yaml", + "version": "v5.0.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "847661e77afa48d99ecfa508e8b60f0b029a19c0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/847661e77afa48d99ecfa508e8b60f0b029a19c0", + "reference": "847661e77afa48d99ecfa508e8b60f0b029a19c0", + "shasum": "" + }, + "require": { + "php": "^7.2.5", + "symfony/polyfill-ctype": "~1.8" + }, + "conflict": { + "symfony/console": "<4.4" + }, + "require-dev": { + "symfony/console": "^4.4|^5.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2019-12-10T11:06:55+00:00" } ], "packages-dev": [], diff --git a/configuration/Car.yaml b/configuration/Car.yaml new file mode 100644 index 0000000..b864fc7 --- /dev/null +++ b/configuration/Car.yaml @@ -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 diff --git a/configuration/Owner.yaml b/configuration/Owner.yaml new file mode 100644 index 0000000..2d69b53 --- /dev/null +++ b/configuration/Owner.yaml @@ -0,0 +1,7 @@ +Owner: + properties: + name: + type: string + serialized_name: name + accessor: + setter: updateOwner diff --git a/index.php b/index.php index ef9451c..c27973b 100644 --- a/index.php +++ b/index.php @@ -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;