| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Traits; |
| 15 |
|
| 16 |
use Carbon\Exceptions\InvalidFormatException; |
| 17 |
use Carbon\FactoryImmutable; |
| 18 |
use DateTimeZone; |
| 19 |
use ReturnTypeWillChange; |
| 20 |
use Throwable; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
trait Serialization |
| 40 |
{ |
| 41 |
use ObjectInitialisation; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected array $dumpProperties = ['date', 'timezone_type', 'timezone']; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected $dumpLocale; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected $dumpDateProperties; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function serialize(): string |
| 69 |
{ |
| 70 |
return serialize($this); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static function fromSerialized($value, array $options = []): static |
| 92 |
{ |
| 93 |
$instance = @unserialize((string) $value, $options); |
| 94 |
|
| 95 |
if (!$instance instanceof static) { |
| 96 |
throw new InvalidFormatException("Invalid serialized value: $value"); |
| 97 |
} |
| 98 |
|
| 99 |
return $instance; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
#[ReturnTypeWillChange] |
| 110 |
public static function __set_state($dump): static |
| 111 |
{ |
| 112 |
if (\is_string($dump)) { |
| 113 |
return static::parse($dump); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
$date = get_parent_class(static::class) && method_exists(parent::class, '__set_state') |
| 118 |
? parent::__set_state((array) $dump) |
| 119 |
: (object) $dump; |
| 120 |
|
| 121 |
return static::instance($date); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function __serialize(): array |
| 130 |
{ |
| 131 |
|
| 132 |
if (isset($this->timezone_type, $this->timezone, $this->date)) { |
| 133 |
return [ |
| 134 |
'date' => $this->date, |
| 135 |
'timezone_type' => $this->timezone_type, |
| 136 |
'timezone' => $this->dumpTimezone($this->timezone), |
| 137 |
]; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
$timezone = $this->getTimezone(); |
| 142 |
$export = [ |
| 143 |
'date' => $this->format('Y-m-d H:i:s.u'), |
| 144 |
'timezone_type' => $timezone->getType(), |
| 145 |
'timezone' => $timezone->getName(), |
| 146 |
]; |
| 147 |
|
| 148 |
|
| 149 |
if (\extension_loaded('msgpack') && isset($this->constructedObjectId)) { |
| 150 |
$timezone = $this->timezone ?? null; |
| 151 |
$export['dumpDateProperties'] = [ |
| 152 |
'date' => $this->format('Y-m-d H:i:s.u'), |
| 153 |
'timezone' => $this->dumpTimezone($timezone), |
| 154 |
]; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
if ($this->localTranslator ?? null) { |
| 159 |
$export['dumpLocale'] = $this->locale ?? null; |
| 160 |
} |
| 161 |
|
| 162 |
return $export; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function __unserialize(array $data): void |
| 169 |
{ |
| 170 |
|
| 171 |
try { |
| 172 |
$this->__construct($data['date'] ?? null, $data['timezone'] ?? null); |
| 173 |
} catch (Throwable $exception) { |
| 174 |
if (!isset($data['dumpDateProperties']['date'], $data['dumpDateProperties']['timezone'])) { |
| 175 |
throw $exception; |
| 176 |
} |
| 177 |
|
| 178 |
try { |
| 179 |
|
| 180 |
['date' => $date, 'timezone' => $timezone] = $data['dumpDateProperties']; |
| 181 |
$this->__construct($date, $timezone); |
| 182 |
} catch (Throwable) { |
| 183 |
throw $exception; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if (isset($data['dumpLocale'])) { |
| 189 |
$this->locale($data['dumpLocale']); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function jsonSerialize(): mixed |
| 197 |
{ |
| 198 |
$serializer = $this->localSerializer |
| 199 |
?? $this->getFactory()->getSettings()['toJsonFormat'] |
| 200 |
?? null; |
| 201 |
|
| 202 |
if ($serializer) { |
| 203 |
return \is_string($serializer) |
| 204 |
? $this->rawFormat($serializer) |
| 205 |
: $serializer($this); |
| 206 |
} |
| 207 |
|
| 208 |
return $this->toJSON(); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public static function serializeUsing(string|callable|null $format): void |
| 218 |
{ |
| 219 |
FactoryImmutable::getDefaultInstance()->serializeUsing($format); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public function cleanupDumpProperties(): self |
| 230 |
{ |
| 231 |
|
| 232 |
if (PHP_VERSION < 8.2) { |
| 233 |
foreach ($this->dumpProperties as $property) { |
| 234 |
if (isset($this->$property)) { |
| 235 |
unset($this->$property); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
return $this; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
private function dumpTimezone(mixed $timezone): mixed |
| 246 |
{ |
| 247 |
return $timezone instanceof DateTimeZone ? $timezone->getName() : $timezone; |
| 248 |
} |
| 249 |
} |