Blame view
public/vendor/nesbot/carbon/src/Carbon/Traits/Serialization.php
8.32 KB
86143e36f Коммит вторник |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
<?php /** * This file is part of the Carbon package. * * (c) Brian Nesbitt <brian@nesbot.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Carbon\Traits; use Carbon\Exceptions\InvalidFormatException; use ReturnTypeWillChange; use Throwable; /** * Trait Serialization. * * Serialization and JSON stuff. * * Depends on the following properties: * * @property int $year * @property int $month * @property int $daysInMonth * @property int $quarter * * Depends on the following methods: * * @method string|static locale(string $locale = null, string ...$fallbackLocales) * @method string toJSON() */ trait Serialization { use ObjectInitialisation; /** * The custom Carbon JSON serializer. * * @var callable|null */ protected static $serializer; /** * List of key to use for dump/serialization. * * @var string[] */ protected $dumpProperties = ['date', 'timezone_type', 'timezone']; /** * Locale to dump comes here before serialization. * * @var string|null */ protected $dumpLocale; /** * Embed date properties to dump in a dedicated variables so it won't overlap native * DateTime ones. * * @var array|null */ protected $dumpDateProperties; /** * Return a serialized string of the instance. * * @return string */ public function serialize() { return serialize($this); } /** * Create an instance from a serialized string. * * @param string $value * * @throws InvalidFormatException * * @return static */ public static function fromSerialized($value) { $instance = @unserialize((string) $value); if (!$instance instanceof static) { throw new InvalidFormatException("Invalid serialized value: $value"); } return $instance; } /** * The __set_state handler. * * @param string|array $dump * * @return static */ #[ReturnTypeWillChange] public static function __set_state($dump) { if (\is_string($dump)) { return static::parse($dump); } /** @var \DateTimeInterface $date */ $date = get_parent_class(static::class) && method_exists(parent::class, '__set_state') ? parent::__set_state((array) $dump) : (object) $dump; return static::instance($date); } /** * Returns the list of properties to dump on serialize() called on. * * Only used by PHP < 7.4. * * @return array */ public function __sleep() { $properties = $this->getSleepProperties(); if ($this->localTranslator ?? null) { $properties[] = 'dumpLocale'; $this->dumpLocale = $this->locale ?? null; } return $properties; } /** * Returns the values to dump on serialize() called on. * * Only used by PHP >= 7.4. * * @return array */ public function __serialize(): array { // @codeCoverageIgnoreStart if (isset($this->timezone_type, $this->timezone, $this->date)) { return [ 'date' => $this->date ?? null, 'timezone_type' => $this->timezone_type, 'timezone' => $this->timezone ?? null, ]; } // @codeCoverageIgnoreEnd $timezone = $this->getTimezone(); $export = [ 'date' => $this->format('Y-m-d H:i:s.u'), 'timezone_type' => $timezone->getType(), 'timezone' => $timezone->getName(), ]; // @codeCoverageIgnoreStart if (\extension_loaded('msgpack') && isset($this->constructedObjectId)) { $export['dumpDateProperties'] = [ 'date' => $this->format('Y-m-d H:i:s.u'), 'timezone' => serialize($this->timezone ?? null), ]; } // @codeCoverageIgnoreEnd if ($this->localTranslator ?? null) { $export['dumpLocale'] = $this->locale ?? null; } return $export; } /** * Set locale if specified on unserialize() called. * * Only used by PHP < 7.4. * * @return void */ #[ReturnTypeWillChange] public function __wakeup() { if (parent::class && method_exists(parent::class, '__wakeup')) { // @codeCoverageIgnoreStart try { parent::__wakeup(); } catch (Throwable $exception) { try { // FatalError occurs when calling msgpack_unpack() in PHP 7.4 or later. ['date' => $date, 'timezone' => $timezone] = $this->dumpDateProperties; parent::__construct($date, unserialize($timezone)); } catch (Throwable $ignoredException) { throw $exception; } } // @codeCoverageIgnoreEnd } $this->constructedObjectId = spl_object_hash($this); if (isset($this->dumpLocale)) { $this->locale($this->dumpLocale); $this->dumpLocale = null; } $this->cleanupDumpProperties(); } /** * Set locale if specified on unserialize() called. * * Only used by PHP >= 7.4. * * @return void */ public function __unserialize(array $data): void { // @codeCoverageIgnoreStart try { $this->__construct($data['date'] ?? null, $data['timezone'] ?? null); } catch (Throwable $exception) { if (!isset($data['dumpDateProperties']['date'], $data['dumpDateProperties']['timezone'])) { throw $exception; } try { // FatalError occurs when calling msgpack_unpack() in PHP 7.4 or later. ['date' => $date, 'timezone' => $timezone] = $data['dumpDateProperties']; $this->__construct($date, unserialize($timezone)); } catch (Throwable $ignoredException) { throw $exception; } } // @codeCoverageIgnoreEnd if (isset($data['dumpLocale'])) { $this->locale($data['dumpLocale']); } } /** * Prepare the object for JSON serialization. * * @return array|string */ #[ReturnTypeWillChange] public function jsonSerialize() { $serializer = $this->localSerializer ?? static::$serializer; if ($serializer) { return \is_string($serializer) ? $this->rawFormat($serializer) : $serializer($this); } return $this->toJSON(); } /** * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. * You should rather transform Carbon object before the serialization. * * JSON serialize all Carbon instances using the given callback. * * @param callable $callback * * @return void */ public static function serializeUsing($callback) { static::$serializer = $callback; } /** * Cleanup properties attached to the public scope of DateTime when a dump of the date is requested. * foreach ($date as $_) {} * serializer($date) * var_export($date) * get_object_vars($date) */ public function cleanupDumpProperties() { // @codeCoverageIgnoreStart if (PHP_VERSION < 8.2) { foreach ($this->dumpProperties as $property) { if (isset($this->$property)) { unset($this->$property); } } } // @codeCoverageIgnoreEnd return $this; } private function getSleepProperties(): array { $properties = $this->dumpProperties; // @codeCoverageIgnoreStart if (!\extension_loaded('msgpack')) { return $properties; } if (isset($this->constructedObjectId)) { $this->dumpDateProperties = [ 'date' => $this->format('Y-m-d H:i:s.u'), 'timezone' => serialize($this->timezone ?? null), ]; $properties[] = 'dumpDateProperties'; } return $properties; // @codeCoverageIgnoreEnd } } |