From c1c45b9172dafbc8f249415ea80b241474d20c68 Mon Sep 17 00:00:00 2001 From: Ruben Date: Sun, 4 Sep 2016 12:36:41 +0100 Subject: [PATCH] Test custom json response --- src/Api/CustomJsonResponse.php | 94 +++++++++++++++++++++++++++++ src/Api/ScoreControllerProvider.php | 7 ++- src/Models/Game.php | 2 +- www/index.php | 3 + 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/Api/CustomJsonResponse.php diff --git a/src/Api/CustomJsonResponse.php b/src/Api/CustomJsonResponse.php new file mode 100644 index 0000000..4697daa --- /dev/null +++ b/src/Api/CustomJsonResponse.php @@ -0,0 +1,94 @@ +serializer_callback = $serializer_callback == null ? [$this, 'serializeDefault'] : $serializer_callback; + + if(!is_callable($this->serializer_callback)) { + throw new \RuntimeException("Invalid serializer callback provided", 1); + } + + if (null === $data) { + $data = new \ArrayObject(); + } + + $this->setData($data); + } + + /** + * {@inheritdoc} + */ + public static function create($data = null, $status = 200, $headers = array()) + { + return new static($data, null, $status, $headers); + } + + /** + * Sets the data to be sent as JSON. + * + * @param mixed $data + * + * @return JsonResponse + * + * @throws \InvalidArgumentException + */ + public function setData($data = array()) + { + $data = call_user_func($this->serializer_callback, $data); + + $this->data = $data; + + return $this->update(); + } + + /** + * Set + * @param [type] $data [description] + * @return [type] [description] + */ + public static function serializeDefault($data) { + if (defined('HHVM_VERSION')) { + // HHVM does not trigger any warnings and let exceptions + // thrown from a JsonSerializable object pass through. + // If only PHP did the same... + $data = json_encode($data, $this->encodingOptions); + } else { + try { + // PHP 5.4 and up wrap exceptions thrown by JsonSerializable + // objects in a new exception that needs to be removed. + // Fortunately, PHP 5.5 and up do not trigger any warning anymore. + $data = json_encode($data, $this->encodingOptions); + } catch (\Exception $e) { + if ('Exception' === get_class($e) && 0 === strpos($e->getMessage(), 'Failed calling ')) { + throw $e->getPrevious() ?: $e; + } + throw $e; + } + } + + if (JSON_ERROR_NONE !== json_last_error()) { + throw new \InvalidArgumentException(json_last_error_msg()); + } + return $data; + } +} \ No newline at end of file diff --git a/src/Api/ScoreControllerProvider.php b/src/Api/ScoreControllerProvider.php index 28db12a..144dd7b 100644 --- a/src/Api/ScoreControllerProvider.php +++ b/src/Api/ScoreControllerProvider.php @@ -47,8 +47,11 @@ class ScoreControllerProvider implements ControllerProviderInterface $game = $request->attributes->get('game'); - return $app['serializer']->serialize($game, 'json'); - + if(empty($game)) { + return new CustomJsonResponse(['message' => 'Game not found'], $app['serializer.json'], 404); + } + + return new CustomJsonResponse($game, $app['serializer.json']); }) ->bind('game') ->convert('game', function($game, Request $request) use ($app){ return $app['entity.manager']->getRepository(Models\Game::class)->find($request->attributes->get('gameId'));}); diff --git a/src/Models/Game.php b/src/Models/Game.php index 183b674..d5ff1fe 100644 --- a/src/Models/Game.php +++ b/src/Models/Game.php @@ -95,7 +95,7 @@ class Game /** * Gets the value of level. - * + * * @return mixed */ public function getLevel() diff --git a/www/index.php b/www/index.php index c4274c2..b9fb59d 100644 --- a/www/index.php +++ b/www/index.php @@ -42,6 +42,9 @@ $app['users'] = function () use ($eh) { $app['serializer'] = function () use ($eh) { return JMS\Serializer\SerializerBuilder::create()->build(); }; +$app['serializer.json'] = function ($data) use ($app) { + return $app['serializer']->serialize($data, 'json'); +}; $app['security.firewalls'] = array( 'login' => [