vendor/pimcore/pimcore/lib/Controller/Traits/JsonHelperTrait.php line 51

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Controller\Traits;
  15. use Pimcore\Bundle\AdminBundle\HttpFoundation\JsonResponse;
  16. use Pimcore\Serializer\Serializer as PimcoreSerializer;
  17. use Psr\Container\ContainerInterface;
  18. /**
  19.  * @property ContainerInterface $container
  20.  */
  21. trait JsonHelperTrait
  22. {
  23.     protected PimcoreSerializer $pimcoreSerializer;
  24.     /**
  25.      * @required
  26.      *
  27.      * @param PimcoreSerializer $pimcoreSerializer
  28.      */
  29.     public function setPimcoreSerializer(PimcoreSerializer $pimcoreSerializer): void
  30.     {
  31.         $this->pimcoreSerializer = $pimcoreSerializer;
  32.     }
  33.     /**
  34.      * Returns a JsonResponse that uses the admin serializer
  35.      *
  36.      * @param mixed $data    The response data
  37.      * @param int $status    The status code to use for the Response
  38.      * @param array $headers Array of extra headers to add
  39.      * @param array $context Context to pass to serializer when using serializer component
  40.      * @param bool $usePimcoreSerializer
  41.      *
  42.      * @return \Symfony\Component\HttpFoundation\JsonResponse
  43.      *
  44.      */
  45.     public function jsonResponse($data, $status = 200, $headers = [], $context = [], bool $usePimcoreSerializer = true)
  46.     {
  47.         $json = $this->encodeJson($data, $context, JsonResponse::DEFAULT_ENCODING_OPTIONS, $usePimcoreSerializer);
  48.         return new JsonResponse($json, $status, $headers, true);
  49.     }
  50.     /**
  51.      * Encodes data into JSON string
  52.      *
  53.      * @param mixed $data    The data to be encoded
  54.      * @param array $context Context to pass to serializer when using serializer component
  55.      * @param int $options   Options passed to json_encode
  56.      * @param bool $usePimcoreSerializer
  57.      *
  58.      * @return string
  59.      */
  60.     public function encodeJson($data, array $context = [], $options = JsonResponse::DEFAULT_ENCODING_OPTIONS, bool $usePimcoreSerializer = true)
  61.     {
  62.         if ($usePimcoreSerializer) {
  63.             $serializer = $this->pimcoreSerializer;
  64.         } else {
  65.             $serializer = $this->container->get('serializer');
  66.         }
  67.         return $serializer->serialize($data, 'json', array_merge([
  68.             'json_encode_options' => $options,
  69.         ], $context));
  70.     }
  71.     /**
  72.      * Decodes a JSON string into an array/object
  73.      *
  74.      * @param mixed $json       The data to be decoded
  75.      * @param bool $associative Whether to decode into associative array or object
  76.      * @param array $context    Context to pass to serializer when using serializer component
  77.      * @param bool $usePimcoreSerializer
  78.      *
  79.      * @return mixed
  80.      */
  81.     public function decodeJson($json, $associative = true, array $context = [], bool $usePimcoreSerializer = true)
  82.     {
  83.         if ($usePimcoreSerializer) {
  84.             $serializer = $this->pimcoreSerializer;
  85.         } else {
  86.             $serializer = $this->container->get('serializer');
  87.         }
  88.         if ($associative) {
  89.             $context['json_decode_associative'] = true;
  90.         }
  91.         // @phpstan-ignore-next-line
  92.         return $serializer->decode($json, 'json', $context);
  93.     }
  94. }