123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?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 InvalidArgumentException;
- /**
- * 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)
- * @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 = null;
- /**
- * 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 \InvalidArgumentException
- *
- * @return static
- */
- public static function fromSerialized($value)
- {
- $instance = @unserialize("$value");
- if (!$instance instanceof static) {
- throw new InvalidArgumentException('Invalid serialized value.');
- }
- return $instance;
- }
- /**
- * The __set_state handler.
- *
- * @param string|array $dump
- *
- * @return static
- */
- 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.
- *
- * @return array
- */
- public function __sleep()
- {
- $properties = $this->dumpProperties;
- if ($this->localTranslator ?? null) {
- $properties[] = 'dumpLocale';
- $this->dumpLocale = $this->locale ?? null;
- }
- return $properties;
- }
- /**
- * Set locale if specified on unserialize() called.
- */
- public function __wakeup()
- {
- if (get_parent_class() && method_exists(parent::class, '__wakeup')) {
- parent::__wakeup();
- }
- $this->constructedObjectId = spl_object_hash($this);
- if (isset($this->dumpLocale)) {
- $this->locale($this->dumpLocale);
- $this->dumpLocale = null;
- }
- $this->cleanupDumpProperties();
- }
- /**
- * Prepare the object for JSON serialization.
- *
- * @return array|string
- */
- public function jsonSerialize()
- {
- $serializer = $this->localSerializer ?? static::$serializer;
- if ($serializer) {
- return is_string($serializer)
- ? $this->rawFormat($serializer)
- : call_user_func($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()
- {
- foreach ($this->dumpProperties as $property) {
- if (isset($this->$property)) {
- unset($this->$property);
- }
- }
- return $this;
- }
- }
|