Source of file Serializer.php
Size: 11,469 Bytes - Last Modified: 2021-01-12T22:04:13+00:00
C:/Users/MAKS/Code/_PROJECTS/amqp-agent/src/Helper/Serializer.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
Covered by 34 test(s):
96
Covered by 34 test(s):
97
Covered by 34 test(s):
98
Covered by 34 test(s):
99100101102103104105106107108109110
Covered by 3 test(s):
111
Covered by 3 test(s):
112
Covered by 2 test(s):
113114115
Covered by 2 test(s):
116
Covered by 1 test(s):
117
Covered by 1 test(s):
118
Covered by 1 test(s):
119
Covered by 1 test(s):
120
Covered by 1 test(s):
121122123
Covered by 1 test(s):
124125126127128
Covered by 1 test(s):
129130131132133134135136137138139140141142
Covered by 7 test(s):
143
Covered by 5 test(s):
144145146
Covered by 7 test(s):
147
Covered by 5 test(s):
148149150
Covered by 6 test(s):
151
Covered by 1 test(s):
152153154
Covered by 6 test(s):
155156
Covered by 2 test(s):
157
Covered by 2 test(s):
158159160
Covered by 6 test(s):
161162
Covered by 6 test(s):
163
Covered by 6 test(s):
164165166
Covered by 6 test(s):
167168169170171172173174175176177178179
Covered by 10 test(s):
180
Covered by 7 test(s):
181182183
Covered by 10 test(s):
184
Covered by 7 test(s):
185186187
Covered by 9 test(s):
188
Covered by 5 test(s):
189190191
Covered by 9 test(s):
192193
Covered by 5 test(s):
194
Covered by 5 test(s):
195196197
Covered by 8 test(s):
198199
Covered by 7 test(s):
200
Covered by 7 test(s):
201202203
Covered by 6 test(s):
204205206207208209210211212213214215216217
Covered by 1 test(s):
218219220221222223224225226227
Covered by 34 test(s):
228229
Covered by 34 test(s):
230231232233234235236237238
Covered by 1 test(s):
239240241242243244245246247248249
Covered by 34 test(s):
250251
Covered by 34 test(s):
252
Covered by 3 test(s):
253
Covered by 3 test(s):
254
Covered by 3 test(s):
255256
Covered by 3 test(s):
257258259260261
Covered by 34 test(s):
262263
Covered by 34 test(s):
264265266267268269270271272
Covered by 1 test(s):
273274275276277278279280281282283
Covered by 34 test(s):
284285
Covered by 34 test(s):
286287288289290291292293294295
Covered by 1 test(s):
296297298299300301302303304305
Covered by 1 test(s):
306307308309310311312313314315
Covered by 1 test(s):
316317318319320321322323324325326327
Covered by 6 test(s):
328329330
Covered by 6 test(s):
331
Covered by 2 test(s):
332
Covered by 2 test(s):
333
Covered by 2 test(s):
334
Covered by 2 test(s):
335
Covered by 2 test(s):
336
Covered by 2 test(s):
337338
Covered by 2 test(s):
339340341342343
Covered by 4 test(s):
344345346347348349350351352353354
Covered by 12 test(s):
355356357
Covered by 12 test(s):
358359360361362363
Covered by 12 test(s):
364
Covered by 9 test(s):
365
Covered by 9 test(s):
366
Covered by 2 test(s):
367
Covered by 2 test(s):
368
Covered by 2 test(s):
369
Covered by 2 test(s):
370371372373374375
Covered by 10 test(s):
376377
| <?php /** * @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com> * @copyright Marwan Al-Soltany 2020 * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ declare(strict_types=1); namespace MAKS\AmqpAgent\Helper; use Exception; use Closure; use MAKS\AmqpAgent\Exception\SerializerViolationException; /** * A flexible serializer to be used in conjunction with the workers. * @since 1.0.0 */ class Serializer { /** * The JSON serialization type constant. * @var string */ public const TYPE_JSON = 'JSON'; /** * The PHP serialization type constant. * @var string */ public const TYPE_PHP = 'PHP'; /** * The default data the serializer works with if none was provided. * @var null */ public const DEFAULT_DATA = null; /** * The default type the serializer works with if none was provided. * @var string */ public const DEFAULT_TYPE = self::TYPE_JSON; /** * The default strict value the serializer works with if none was provided. * @var bool */ public const DEFAULT_STRICT = true; /** * The supported serialization types. * @var array */ protected const SUPPORTED_TYPES = [self::TYPE_JSON, self::TYPE_PHP]; /** * The current data the serializer has. * @var mixed */ protected $data; /** * The current type the serializer uses. * @var string */ protected $type; /** * The current strict value the serializer works with. * @var bool */ protected $strict; /** * The result of the last (un)serialization operation. * @var mixed */ protected $result; /** * Serializer object constructor. * @param mixed $data [optional] The data to (un)serialize. Defaults to null. * @param string|null $type [optional] The type of (un)serialization. Defaults to JSON. * @param bool $strict [optional] Whether or not to assert that no errors have occurred while executing (un)serialization functions. Defaults to true. * @throws SerializerViolationException */ public function __construct($data = null, ?string $type = null, ?bool $strict = null) { $this->setData($data ?? self::DEFAULT_DATA); $this->setType($type ?? self::DEFAULT_TYPE); $this->setStrict($strict ?? self::DEFAULT_STRICT); } /** * Executes when calling the class like a function. * @param mixed $data The data to (un)serialize. * @param string|null $type [optional] The type of (un)serialization. Defaults to JSON. * @param bool $strict [optional] Whether or not to assert that no errors have occurred while executing (un)serialization functions. Defaults to true. * @return mixed Serialized or unserialized data depending on the passed parameters. * @throws SerializerViolationException */ public function __invoke($data, ?string $type = self::DEFAULT_TYPE, ?bool $strict = self::DEFAULT_STRICT) { $this->setData($data); $this->setType($type ?? self::DEFAULT_TYPE); $this->setStrict($strict ?? self::DEFAULT_STRICT); try { $this->result = is_string($data) ? $this->unserialize() : $this->serialize(); } catch (Exception $error) { $dataType = gettype($data); throw new SerializerViolationException( sprintf( 'The data passed to the serializer (data-type: %s) could not be processed!', $dataType ), (int)$error->getCode(), $error ); } return $this->result; } /** * Serializes the passed or registered data. When no parameters are passed, it uses the registered ones. * @param mixed $data [optional] The data to serialize. * @param string|null $type [optional] The type of serialization. * @param bool $strict [optional] Whether or not to assert that no errors have occurred while executing serialization functions. * @return string|null A serialized representation of the passed or registered data or null on failure. * @throws SerializerViolationException */ public function serialize($data = null, ?string $type = null, ?bool $strict = null): string { if (null !== $data) { $this->setData($data); } if (null !== $type) { $this->setType($type); } if (null !== $strict) { $this->setStrict($strict); } if (self::TYPE_PHP === $this->type) { $this->assertNoPhpSerializationError(function () { $this->result = serialize($this->data); }); } if (self::TYPE_JSON === $this->type) { $this->assertNoJsonSerializationError(function () { $this->result = json_encode($this->data); }); } return $this->result; } /** * Unserializes the passed or registered data. When no parameters are passed, it uses the registered ones. * @param string|null $data [optional] The data to unserialize. * @param string|null $type [optional] The type of unserialization. * @param bool $strict [optional] Whether or not to assert that no errors have occurred while executing unserialization functions. * @return mixed A PHP type on success or false or null on failure. * @throws SerializerViolationException */ public function unserialize(?string $data = null, ?string $type = null, ?bool $strict = null) { if (null !== $data) { $this->setData($data); } if (null !== $type) { $this->setType($type); } if (null !== $strict) { $this->setStrict($strict); } if (self::TYPE_PHP === $this->type) { $this->assertNoPhpSerializationError(function () { $this->result = unserialize($this->data); }); } if (self::TYPE_JSON === $this->type) { $this->assertNoJsonSerializationError(function () { $this->result = json_decode($this->data, true); }); } return $this->result; } /** * Deserializes the passed or registered data. When no parameters are passed, it uses the registered ones. * @since 1.2.2 Alias for `self::unserialize()`. * @param string|null $data [optional] The data to unserialize. * @param string|null $type [optional] The type of unserialization. * @param bool $strict [optional] Whether or not to assert that no errors have occurred while executing unserialization functions. * @return mixed A PHP type on success or false or null on failure. * @throws SerializerViolationException */ public function deserialize(?string $data = null, ?string $type = null, ?bool $strict = null) { return $this->unserialize($data, $type, $strict); } /** * Registers the passed data in the object. * @param mixed $data The data wished to be registered. * @return self */ public function setData($data) { $this->data = $data; return $this; } /** * Returns the currently registered data. * @return mixed */ public function getData() { return $this->data; } /** * Registers the passed type in the object. * @param string $type The type wished to be registered. * @return self * @throws SerializerViolationException */ public function setType(string $type) { $type = strtoupper($type); if (!in_array($type, static::SUPPORTED_TYPES)) { throw new SerializerViolationException( sprintf( '"%s" is unsupported (un)serialization type. Supported types are: [%s]!', $type, implode(', ', static::SUPPORTED_TYPES) ) ); } $this->type = $type; return $this; } /** * Returns the currently registered type. * @return string */ public function getType(): string { return $this->type; } /** * Registers the passed strict value in the object. * @since 1.2.2 * @param bool $strict The strict value wished to be registered. * @return self */ public function setStrict(bool $strict) { $this->strict = $strict; return $this; } /** * Returns the currently registered strict value. * @since 1.2.2 * @return bool */ public function isStrict() { return $this->strict; } /** * Alias for `self::serialize()` that does not accept any parameters (works with currently registered parameters). * @return string The serialized data. * @throws SerializerViolationException */ public function getSerialized(): string { return $this->serialize(); } /** * Alias for `self::unserialize()` that does not accept any parameters (works with currently registered parameters). * @return mixed The unserialized data. * @throws SerializerViolationException */ public function getUnserialized() { return $this->unserialize(); } /** * Asserts that `serialize()` and/or `unserialize()` was executed successfully depending on strictness of the Serializer. * @since 1.2.2 * @param Closure $callback The (un)serialization callback to execute. * @return void * @throws SerializerViolationException */ protected function assertNoPhpSerializationError(Closure $callback): void { $this->result = null; try { $callback(); } catch (Exception $error) { if ($this->strict) { throw new SerializerViolationException( sprintf( 'An error occurred while executing serialize() or unserialize(): %s', (string)$error->getMessage() ), (int)$error->getCode(), $error ); } } } /** * Asserts that `json_encode()` and/or `json_decode()` was executed successfully depending on strictness of the Serializer. * @since 1.2.2 * @param Closure $callback The (un)serialization callback to execute. * @return void * @throws SerializerViolationException */ protected function assertNoJsonSerializationError(Closure $callback): void { $this->result = null; try { $callback(); } catch (Exception $error) { // JSON functions do not throw exceptions on PHP < v7.3.0 // The code down below takes care of throwing the exception. } if ($this->strict) { $errorCode = json_last_error(); if ($errorCode !== JSON_ERROR_NONE) { $errorMessage = json_last_error_msg(); throw new SerializerViolationException( sprintf( 'An error occurred while executing json_encode() or json_decode(): %s', $errorMessage ) ); } } } } |