Skip to content

Commit

Permalink
Added functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sorinsarca committed Dec 26, 2024
1 parent 77541aa commit eeb9c34
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Serialize anything
including closures, without breaking a sweat.

```php
use Opis\Closure\Serializer;
use function Opis\Closure\{serialize, unserialize};

$serialized = Serializer::serialize(fn() => "hello!");
$greet = Serializer::unserialize($serialized);
$serialized = serialize(fn() => "hello!");
$greet = unserialize($serialized);

echo $greet(); // hello
```
Expand Down
4 changes: 3 additions & 1 deletion autoload.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

spl_autoload_register(static function ($class) {
require_once __DIR__ . '/functions.php';

spl_autoload_register(static function (string $class): bool {
$class = ltrim($class, '\\');
$dir = __DIR__ . '/src';
$namespace = 'Opis\Closure';
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"autoload": {
"psr-4": {
"Opis\\Closure\\": "src/"
}
},
"files": ["functions.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
19 changes: 19 additions & 0 deletions functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Opis\Closure;

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
{
return Serializer::serialize($data, $security);
}

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

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

/**
* @throws SecurityException
*/
public static function unserialize(string $data, ?SecurityProviderInterface $security = null): mixed
{
self::$init || self::init();
Expand Down

0 comments on commit eeb9c34

Please sign in to comment.