vendor/pimcore/pimcore/lib/Tool/Serialize.php line 43

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\Tool;
  15. final class Serialize
  16. {
  17.     /**
  18.      * @var array
  19.      */
  20.     protected static $loopFilterProcessedObjects = [];
  21.     /**
  22.      * @param mixed $data
  23.      *
  24.      * @return string
  25.      */
  26.     public static function serialize($data)
  27.     {
  28.         return serialize($data);
  29.     }
  30.     /**
  31.      * @param string $data
  32.      *
  33.      * @return mixed
  34.      */
  35.     public static function unserialize($data)
  36.     {
  37.         if (!empty($data) && is_string($data)) {
  38.             $data unserialize($data);
  39.         }
  40.         return $data;
  41.     }
  42.     /**
  43.      * @internal
  44.      *
  45.      * Shortcut to access the admin serializer
  46.      *
  47.      * @return \Symfony\Component\Serializer\Serializer
  48.      */
  49.     public static function getAdminSerializer()
  50.     {
  51.         return \Pimcore::getContainer()->get('pimcore_admin.serializer');
  52.     }
  53.     /**
  54.      * @internal
  55.      *
  56.      * this is a special json encoder that avoids recursion errors
  57.      * especially for pimcore models that contain massive self referencing objects
  58.      *
  59.      * @param mixed $data
  60.      *
  61.      * @return mixed
  62.      */
  63.     public static function removeReferenceLoops($data): mixed
  64.     {
  65.         self::$loopFilterProcessedObjects = []; // reset
  66.         return self::loopFilterCycles($data);
  67.     }
  68.     /**
  69.      * @param mixed $element
  70.      *
  71.      * @return mixed
  72.      */
  73.     protected static function loopFilterCycles($element)
  74.     {
  75.         if (is_array($element)) {
  76.             foreach ($element as &$value) {
  77.                 $value self::loopFilterCycles($value);
  78.             }
  79.         } elseif (is_object($element)) {
  80.             try {
  81.                 $clone = clone $element// do not modify the original object
  82.             } catch (\Throwable $e) {
  83.                 return sprintf('"* NON-CLONEABLE (%s): %s *"'get_class($element), $e->getMessage());
  84.             }
  85.             if (in_array($elementself::$loopFilterProcessedObjectstrue)) {
  86.                 return '"* RECURSION (' get_class($element) . ') *"';
  87.             }
  88.             self::$loopFilterProcessedObjects[] = $element;
  89.             $propCollection get_object_vars($clone);
  90.             foreach ($propCollection as $name => $propValue) {
  91.                 if (!str_starts_with($name"\0")) {
  92.                     $clone->$name self::loopFilterCycles($propValue);
  93.                 }
  94.             }
  95.             array_splice(self::$loopFilterProcessedObjectsarray_search($elementself::$loopFilterProcessedObjectstrue), 1);
  96.             return $clone;
  97.         }
  98.         return $element;
  99.     }
  100. }