-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce serializer and introduce json serialization instead of php …
…serialization
- Loading branch information
Showing
10 changed files
with
439 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\TagBag\Serializer; | ||
|
||
use Setono\TagBag\Exception\SerializationException; | ||
|
||
final class CompositeSerializer implements SerializerInterface | ||
{ | ||
/** @var list<SerializerInterface> */ | ||
private array $serializers = []; | ||
|
||
public function __construct(SerializerInterface ...$serializers) | ||
{ | ||
foreach ($serializers as $serializer) { | ||
$this->add($serializer); | ||
} | ||
} | ||
|
||
public function add(SerializerInterface $serializer): void | ||
{ | ||
$this->serializers[] = $serializer; | ||
} | ||
|
||
public function serialize(array $tags): string | ||
{ | ||
foreach ($this->serializers as $serializer) { | ||
try { | ||
return $serializer->serialize($tags); | ||
} catch (SerializationException) { | ||
continue; | ||
} | ||
} | ||
|
||
throw new SerializationException('No serializer was able to serialize the tags'); | ||
} | ||
|
||
public function deserialize(string $data): array | ||
{ | ||
foreach ($this->serializers as $serializer) { | ||
try { | ||
return $serializer->deserialize($data); | ||
} catch (SerializationException) { | ||
continue; | ||
} | ||
} | ||
|
||
throw new SerializationException('No serializer was able to deserialize the data'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\TagBag\Serializer; | ||
|
||
use JsonException; | ||
use Setono\TagBag\Exception\SerializationException; | ||
use Setono\TagBag\Tag\RenderedTag; | ||
use Throwable; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class JsonSerializer implements SerializerInterface | ||
{ | ||
public function serialize(array $tags): string | ||
{ | ||
try { | ||
return json_encode($tags, \JSON_THROW_ON_ERROR); | ||
} catch (JsonException $e) { | ||
throw new SerializationException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
|
||
public function deserialize(string $data): array | ||
{ | ||
try { | ||
/** @var mixed $data */ | ||
$data = json_decode($data, true, 512, \JSON_THROW_ON_ERROR); | ||
Check warning on line 28 in src/Serializer/JsonSerializer.php GitHub Actions / Mutation tests (8.3, highest)
Check warning on line 28 in src/Serializer/JsonSerializer.php GitHub Actions / Mutation tests (8.3, highest)
|
||
Assert::isArray($data); | ||
Check warning on line 29 in src/Serializer/JsonSerializer.php GitHub Actions / Mutation tests (8.3, highest)
|
||
|
||
$tags = []; | ||
|
||
foreach ($data as $section => $sectionTags) { | ||
Assert::string($section); | ||
Check warning on line 34 in src/Serializer/JsonSerializer.php GitHub Actions / Mutation tests (8.3, highest)
|
||
Assert::isArray($sectionTags); | ||
Check warning on line 35 in src/Serializer/JsonSerializer.php GitHub Actions / Mutation tests (8.3, highest)
|
||
|
||
/** @var mixed $tag */ | ||
foreach ($sectionTags as $tag) { | ||
Assert::isArray($tag); | ||
|
||
$tags[$section][] = RenderedTag::createFromArray($tag); | ||
} | ||
} | ||
|
||
return $tags; | ||
} catch (Throwable $e) { | ||
throw new SerializationException( | ||
message: $e->getMessage(), | ||
previous: $e, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\TagBag\Serializer; | ||
|
||
use Setono\TagBag\Exception\SerializationException; | ||
use Setono\TagBag\Tag\RenderedTag; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @deprecated since 2.3 and will be removed in 3.0. Use the `JsonSerializer` instead | ||
*/ | ||
final class PhpSerializer implements SerializerInterface | ||
{ | ||
public function serialize(array $tags): string | ||
{ | ||
return serialize($tags); | ||
} | ||
|
||
/** | ||
* Most of this method is taken from here: https://github.com/symfony/symfony/blob/6.2/src/Symfony/Component/Messenger/Transport/Serialization/PhpSerializer.php | ||
*/ | ||
public function deserialize(string $data): array | ||
{ | ||
if ('' === $data) { | ||
throw SerializationException::emptyData(); | ||
} | ||
|
||
$serializationException = new SerializationException(sprintf('Could not unserialize data: %s.', $data)); | ||
$prevUnserializeHandler = ini_set('unserialize_callback_func', self::class . '::handleUnserializeCallback'); | ||
/** @psalm-suppress MixedArgumentTypeCoercion,UndefinedVariable */ | ||
$prevErrorHandler = set_error_handler(static function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $serializationException) { | ||
if (__FILE__ === $file) { | ||
throw $serializationException; | ||
} | ||
|
||
/** @psalm-suppress MixedFunctionCall */ | ||
return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false; | ||
}); | ||
|
||
try { | ||
/** @var array<string, list<RenderedTag>> $result */ | ||
$result = unserialize($data, [ | ||
'allowed_classes' => [RenderedTag::class], | ||
]); | ||
/** @psalm-suppress RedundantConditionGivenDocblockType */ | ||
Assert::isArray($result); | ||
foreach ($result as $section => $tags) { | ||
/** @psalm-suppress RedundantConditionGivenDocblockType */ | ||
Assert::string($section); | ||
|
||
/** @psalm-suppress RedundantConditionGivenDocblockType */ | ||
Assert::isArray($tags); | ||
|
||
Assert::allIsInstanceOf($tags, RenderedTag::class); | ||
} | ||
} catch (\InvalidArgumentException) { | ||
throw new SerializationException(sprintf('The unserialized data was incorrect. Here is the original data: %s', $data)); | ||
} finally { | ||
restore_error_handler(); | ||
ini_set('unserialize_callback_func', $prevUnserializeHandler); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public static function handleUnserializeCallback(string $class): never | ||
{ | ||
throw new SerializationException(sprintf('Message class "%s" not found during decoding.', $class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\TagBag\Serializer; | ||
|
||
use Setono\TagBag\Exception\SerializationException; | ||
use Setono\TagBag\Tag\RenderedTag; | ||
|
||
interface SerializerInterface | ||
{ | ||
/** | ||
* @param array<string, list<RenderedTag>> $tags | ||
* | ||
* @throws SerializationException if the serialization fails | ||
*/ | ||
public function serialize(array $tags): string; | ||
|
||
/** | ||
* @return array<string, list<RenderedTag>> | ||
* | ||
* @throws SerializationException if the deserialization fails | ||
*/ | ||
public function deserialize(string $data): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.