Skip to content

Commit

Permalink
Added unserialize options
Browse files Browse the repository at this point in the history
  • Loading branch information
sorinsarca committed Dec 27, 2024
1 parent eeb9c34 commit e08bbc0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
22 changes: 18 additions & 4 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@

namespace Opis\Closure;

function init(Security\SecurityProviderInterface|string|null $security = null, bool $v3Compatible = false): void
function init(
Security\SecurityProviderInterface|string|null $security = null,
bool $v3Compatible = false
): void
{
Serializer::init($security, $v3Compatible);
}

function serialize(mixed $data, ?Security\SecurityProviderInterface $security = null): string
function serialize(
mixed $data,
?Security\SecurityProviderInterface $security = null
): string
{
return Serializer::serialize($data, $security);
}

function unserialize(string $data, ?Security\SecurityProviderInterface $security = null): mixed
function unserialize(
string $data,
Security\SecurityProviderInterface|array|null $security = null,
?array $options = null
): mixed
{
return Serializer::unserialize($data, $security);
if (is_array($security)) {
// this is only for compatibility with v3
return Serializer::unserialize($data, null, $security);
}
return Serializer::unserialize($data, $security, $options);
}

8 changes: 7 additions & 1 deletion src/DeserializationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ class DeserializationHandler
private ?WeakMap $unboxed = null;
private ?WeakMap $refs = null;
private ?array $visitedArrays = null;
private array $options;

public function __construct(?array $options = null)
{
$this->options = $options ?? [];
}

public function unserialize(string $serialized): mixed
{
Expand All @@ -23,7 +29,7 @@ public function unserialize(string $serialized): mixed
}

try {
$data = unserialize($serialized);
$data = unserialize($serialized, $this->options);
unset($serialized);

// handle unboxing
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static function serialize(mixed $data, ?SecurityProviderInterface $securi
return self::encode((new SerializationHandler())->serialize($data), $security);
}

public static function unserialize(string $data, ?SecurityProviderInterface $security = null): mixed
public static function unserialize(string $data, ?SecurityProviderInterface $security = null, ?array $options = null): mixed
{
self::$init || self::init();

Expand All @@ -141,7 +141,7 @@ public static function unserialize(string $data, ?SecurityProviderInterface $sec
}

// Create a new deserialization handler
$handler = new DeserializationHandler();
$handler = new DeserializationHandler($options);

if (!$skipDecode) {
// current - v4
Expand Down

0 comments on commit e08bbc0

Please sign in to comment.