diff --git a/bootstrap.php b/bootstrap.php index d474d97..8082a25 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -1,13 +1,18 @@ configureListeners(function (EventDispatcher $dispatcher) { + $dispatcher->addSubscriber(new CarEventSubscriber()); + }) ->build(); final class Car diff --git a/composer.json b/composer.json index 600c9d3..1a93dad 100644 --- a/composer.json +++ b/composer.json @@ -6,5 +6,10 @@ "platform": { "php": "7.3.*" } + }, + "autoload": { + "psr-4": { + "App\\": "src/" + } } } diff --git a/index.php b/index.php index ef9451c..136413a 100644 --- a/index.php +++ b/index.php @@ -3,9 +3,10 @@ require_once __DIR__ . '/bootstrap.php'; //Serialize -$opelAstra = new Car('Opel', 'Astra'); -$jsonData = $serializer->serialize($opelAstra, 'json'); -$xmlData = $serializer->serialize($opelAstra, 'xml'); +$car1 = new Car('Opel', 'Astra'); +$car2 = new Car('Audi', 'A3'); +$jsonData = $serializer->serialize($car1, 'json'); +$xmlData = $serializer->serialize($car2, 'xml'); //Schrijf het resultaat naar een file file_put_contents("xmldata.xml", $xmlData); diff --git a/src/CarEventSubscriber.php b/src/CarEventSubscriber.php new file mode 100644 index 0000000..cefa3eb --- /dev/null +++ b/src/CarEventSubscriber.php @@ -0,0 +1,75 @@ +json + return [ + [ + 'event' => DefinedEvents::PRE_SERIALIZE, + 'method' => 'onPreSerialize', + 'class' => Car::class, + 'priority' => 0, + ],[ + 'event' => DefinedEvents::POST_SERIALIZE, + 'method' => 'onPostSerialize', + 'class' => Car::class, + 'priority' => 0, + ],[ + 'event' => DefinedEvents::PRE_DESERIALIZE, + 'method' => 'onPreDeserialize', + 'class' => Car::class, + 'priority' => 0, + ],[ + 'event' => DefinedEvents::POST_DESERIALIZE, + 'method' => 'onPostDeserialize', + 'class' => Car::class, + 'priority' => 0, + ], + ]; + } + + + public function onPreSerialize(PreSerializeEvent $event) + { + echo __FUNCTION__ . ' ON ' . get_class($event->getObject()); + echo __FILE__ . ':' . __LINE__ . PHP_EOL; + echo '
'; + } + + public function onPostSerialize(ObjectEvent $event) + { + echo __FUNCTION__ . ' ON ' . get_class($event->getObject()); + echo __FILE__ . ':' . __LINE__ . PHP_EOL; + echo '
'; + } + + public function onPreDeserialize(PreDeserializeEvent $event) + { + echo __FUNCTION__; + echo '
'; + echo print_r($event->getData()); + echo __FILE__ . ':' . __LINE__ . PHP_EOL; + echo '
'; + } + + public function onPostDeserialize(ObjectEvent $event) + { +// echo __FUNCTION__ . ' ON ' . get_class($event->getObject()); + echo __FILE__ . ':' . __LINE__ . PHP_EOL; + echo '
'; + } +} diff --git a/src/DefinedEvents.php b/src/DefinedEvents.php new file mode 100644 index 0000000..be7e04c --- /dev/null +++ b/src/DefinedEvents.php @@ -0,0 +1,16 @@ +