Boundaries.php
4 years ago
Cast.php
4 years ago
Comparison.php
2 years ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
9 months ago
DeprecatedProperties.php
4 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
4 years ago
Localization.php
9 months ago
Macro.php
4 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
4 years ago
Mutability.php
4 years ago
ObjectInitialisation.php
4 years ago
Options.php
2 years ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
9 months ago
Timestamp.php
1 year ago
ToStringFormat.php
3 years ago
Units.php
2 years ago
Week.php
4 years ago
index.php
3 years ago
Serialization.php
151 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon\Traits; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Carbon\Exceptions\InvalidFormatException; |
| 5 | use ReturnTypeWillChange; |
| 6 | use Throwable; |
| 7 | trait Serialization |
| 8 | { |
| 9 | use ObjectInitialisation; |
| 10 | protected static $serializer; |
| 11 | protected $dumpProperties = ['date', 'timezone_type', 'timezone']; |
| 12 | protected $dumpLocale; |
| 13 | protected $dumpDateProperties; |
| 14 | public function serialize() |
| 15 | { |
| 16 | return \serialize($this); |
| 17 | } |
| 18 | public static function fromSerialized($value) |
| 19 | { |
| 20 | $instance = @\unserialize((string) $value); |
| 21 | if (!$instance instanceof static) { |
| 22 | throw new InvalidFormatException("Invalid serialized value: {$value}"); |
| 23 | } |
| 24 | return $instance; |
| 25 | } |
| 26 | #[\ReturnTypeWillChange] |
| 27 | public static function __set_state($dump) |
| 28 | { |
| 29 | if (\is_string($dump)) { |
| 30 | return static::parse($dump); |
| 31 | } |
| 32 | $date = \get_parent_class(static::class) && \method_exists(parent::class, '__set_state') ? parent::__set_state((array) $dump) : (object) $dump; |
| 33 | return static::instance($date); |
| 34 | } |
| 35 | public function __sleep() |
| 36 | { |
| 37 | $properties = $this->getSleepProperties(); |
| 38 | if ($this->localTranslator ?? null) { |
| 39 | $properties[] = 'dumpLocale'; |
| 40 | $this->dumpLocale = $this->locale ?? null; |
| 41 | } |
| 42 | return $properties; |
| 43 | } |
| 44 | public function __serialize() : array |
| 45 | { |
| 46 | // @codeCoverageIgnoreStart |
| 47 | if (isset($this->timezone_type, $this->timezone, $this->date)) { |
| 48 | return ['date' => $this->date ?? null, 'timezone_type' => $this->timezone_type, 'timezone' => $this->timezone ?? null]; |
| 49 | } |
| 50 | // @codeCoverageIgnoreEnd |
| 51 | $timezone = $this->getTimezone(); |
| 52 | $export = ['date' => $this->format('Y-m-d H:i:s.u'), 'timezone_type' => $timezone->getType(), 'timezone' => $timezone->getName()]; |
| 53 | // @codeCoverageIgnoreStart |
| 54 | if (\extension_loaded('msgpack') && isset($this->constructedObjectId)) { |
| 55 | $export['dumpDateProperties'] = ['date' => $this->format('Y-m-d H:i:s.u'), 'timezone' => \serialize($this->timezone ?? null)]; |
| 56 | } |
| 57 | // @codeCoverageIgnoreEnd |
| 58 | if ($this->localTranslator ?? null) { |
| 59 | $export['dumpLocale'] = $this->locale ?? null; |
| 60 | } |
| 61 | return $export; |
| 62 | } |
| 63 | #[\ReturnTypeWillChange] |
| 64 | public function __wakeup() |
| 65 | { |
| 66 | if (parent::class && \method_exists(parent::class, '__wakeup')) { |
| 67 | // @codeCoverageIgnoreStart |
| 68 | try { |
| 69 | parent::__wakeup(); |
| 70 | } catch (Throwable $exception) { |
| 71 | try { |
| 72 | // FatalError occurs when calling msgpack_unpack() in PHP 7.4 or later. |
| 73 | ['date' => $date, 'timezone' => $timezone] = $this->dumpDateProperties; |
| 74 | parent::__construct($date, \unserialize($timezone)); |
| 75 | } catch (Throwable $ignoredException) { |
| 76 | throw $exception; |
| 77 | } |
| 78 | } |
| 79 | // @codeCoverageIgnoreEnd |
| 80 | } |
| 81 | $this->constructedObjectId = \spl_object_hash($this); |
| 82 | if (isset($this->dumpLocale)) { |
| 83 | $this->locale($this->dumpLocale); |
| 84 | $this->dumpLocale = null; |
| 85 | } |
| 86 | $this->cleanupDumpProperties(); |
| 87 | } |
| 88 | public function __unserialize(array $data) : void |
| 89 | { |
| 90 | // @codeCoverageIgnoreStart |
| 91 | try { |
| 92 | $this->__construct($data['date'] ?? null, $data['timezone'] ?? null); |
| 93 | } catch (Throwable $exception) { |
| 94 | if (!isset($data['dumpDateProperties']['date'], $data['dumpDateProperties']['timezone'])) { |
| 95 | throw $exception; |
| 96 | } |
| 97 | try { |
| 98 | // FatalError occurs when calling msgpack_unpack() in PHP 7.4 or later. |
| 99 | ['date' => $date, 'timezone' => $timezone] = $data['dumpDateProperties']; |
| 100 | $this->__construct($date, \unserialize($timezone)); |
| 101 | } catch (Throwable $ignoredException) { |
| 102 | throw $exception; |
| 103 | } |
| 104 | } |
| 105 | // @codeCoverageIgnoreEnd |
| 106 | if (isset($data['dumpLocale'])) { |
| 107 | $this->locale($data['dumpLocale']); |
| 108 | } |
| 109 | } |
| 110 | #[\ReturnTypeWillChange] |
| 111 | public function jsonSerialize() |
| 112 | { |
| 113 | $serializer = $this->localSerializer ?? static::$serializer; |
| 114 | if ($serializer) { |
| 115 | return \is_string($serializer) ? $this->rawFormat($serializer) : $serializer($this); |
| 116 | } |
| 117 | return $this->toJSON(); |
| 118 | } |
| 119 | public static function serializeUsing($callback) |
| 120 | { |
| 121 | static::$serializer = $callback; |
| 122 | } |
| 123 | public function cleanupDumpProperties() |
| 124 | { |
| 125 | // @codeCoverageIgnoreStart |
| 126 | if (\PHP_VERSION < 8.199999999999999) { |
| 127 | foreach ($this->dumpProperties as $property) { |
| 128 | if (isset($this->{$property})) { |
| 129 | unset($this->{$property}); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | // @codeCoverageIgnoreEnd |
| 134 | return $this; |
| 135 | } |
| 136 | private function getSleepProperties() : array |
| 137 | { |
| 138 | $properties = $this->dumpProperties; |
| 139 | // @codeCoverageIgnoreStart |
| 140 | if (!\extension_loaded('msgpack')) { |
| 141 | return $properties; |
| 142 | } |
| 143 | if (isset($this->constructedObjectId)) { |
| 144 | $this->dumpDateProperties = ['date' => $this->format('Y-m-d H:i:s.u'), 'timezone' => \serialize($this->timezone ?? null)]; |
| 145 | $properties[] = 'dumpDateProperties'; |
| 146 | } |
| 147 | return $properties; |
| 148 | // @codeCoverageIgnoreEnd |
| 149 | } |
| 150 | } |
| 151 |