1
<?php
2

3
/**
4
 * This file is part of the Carbon package.
5
 *
6
 * (c) Brian Nesbitt <brian@nesbot.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Carbon\Traits;
12

13
use Carbon\Exceptions\InvalidFormatException;
14

15
/**
16
 * Trait Serialization.
17
 *
18
 * Serialization and JSON stuff.
19
 *
20
 * Depends on the following properties:
21
 *
22
 * @property int $year
23
 * @property int $month
24
 * @property int $daysInMonth
25
 * @property int $quarter
26
 *
27
 * Depends on the following methods:
28
 *
29
 * @method string|static locale(string $locale = null, string ...$fallbackLocales)
30
 * @method string        toJSON()
31
 */
32
trait Serialization
33
{
34
    use ObjectInitialisation;
35

36
    /**
37
     * The custom Carbon JSON serializer.
38
     *
39
     * @var callable|null
40
     */
41
    protected static $serializer;
42

43
    /**
44
     * List of key to use for dump/serialization.
45
     *
46
     * @var string[]
47
     */
48
    protected $dumpProperties = ['date', 'timezone_type', 'timezone'];
49

50
    /**
51
     * Locale to dump comes here before serialization.
52
     *
53
     * @var string|null
54
     */
55
    protected $dumpLocale = null;
56

57
    /**
58
     * Return a serialized string of the instance.
59
     *
60
     * @return string
61
     */
62 1
    public function serialize()
63
    {
64 1
        return serialize($this);
65
    }
66

67
    /**
68
     * Create an instance from a serialized string.
69
     *
70
     * @param string $value
71
     *
72
     * @throws InvalidFormatException
73
     *
74
     * @return static
75
     */
76 1
    public static function fromSerialized($value)
77
    {
78 1
        $instance = @unserialize("$value");
79

80 1
        if (!$instance instanceof static) {
81 1
            throw new InvalidFormatException("Invalid serialized value: $value");
82
        }
83

84 1
        return $instance;
85
    }
86

87
    /**
88
     * The __set_state handler.
89
     *
90
     * @param string|array $dump
91
     *
92
     * @return static
93
     */
94 1
    public static function __set_state($dump)
95
    {
96 1
        if (\is_string($dump)) {
97 1
            return static::parse($dump);
98
        }
99

100
        /** @var \DateTimeInterface $date */
101 1
        $date = get_parent_class(static::class) && method_exists(parent::class, '__set_state')
102 1
            ? parent::__set_state((array) $dump)
103 1
            : (object) $dump;
104

105 1
        return static::instance($date);
106
    }
107

108
    /**
109
     * Returns the list of properties to dump on serialize() called on.
110
     *
111
     * @return array
112
     */
113 1
    public function __sleep()
114
    {
115 1
        $properties = $this->dumpProperties;
116

117 1
        if ($this->localTranslator ?? null) {
118 1
            $properties[] = 'dumpLocale';
119 1
            $this->dumpLocale = $this->locale ?? null;
120
        }
121

122 1
        return $properties;
123
    }
124

125
    /**
126
     * Set locale if specified on unserialize() called.
127
     */
128 1
    public function __wakeup()
129
    {
130 1
        if (get_parent_class() && method_exists(parent::class, '__wakeup')) {
131 1
            parent::__wakeup();
132
        }
133

134 1
        $this->constructedObjectId = spl_object_hash($this);
135

136 1
        if (isset($this->dumpLocale)) {
137 1
            $this->locale($this->dumpLocale);
138 1
            $this->dumpLocale = null;
139
        }
140

141 1
        $this->cleanupDumpProperties();
142
    }
143

144
    /**
145
     * Prepare the object for JSON serialization.
146
     *
147
     * @return array|string
148
     */
149 1
    public function jsonSerialize()
150
    {
151 1
        $serializer = $this->localSerializer ?? static::$serializer;
152 1
        if ($serializer) {
153 1
            return \is_string($serializer)
154 1
                ? $this->rawFormat($serializer)
155 1
                : \call_user_func($serializer, $this);
156
        }
157

158 1
        return $this->toJSON();
159
    }
160

161
    /**
162
     * @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
163
     *             You should rather transform Carbon object before the serialization.
164
     *
165
     * JSON serialize all Carbon instances using the given callback.
166
     *
167
     * @param callable $callback
168
     *
169
     * @return void
170
     */
171 1
    public static function serializeUsing($callback)
172
    {
173 1
        static::$serializer = $callback;
174
    }
175

176
    /**
177
     * Cleanup properties attached to the public scope of DateTime when a dump of the date is requested.
178
     * foreach ($date as $_) {}
179
     * serializer($date)
180
     * var_export($date)
181
     * get_object_vars($date)
182
     */
183 1
    public function cleanupDumpProperties()
184
    {
185 1
        foreach ($this->dumpProperties as $property) {
186 1
            if (isset($this->$property)) {
187 1
                unset($this->$property);
188
            }
189
        }
190

191 1
        return $this;
192
    }
193
}

Read our documentation on viewing source code .

Loading