Skip to content

Commit

Permalink
feat(uniqueId): for change driver per usage
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Jul 30, 2024
1 parent 7b9c575 commit 5a322a3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 25 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,51 @@ In your project are available two new methods.

If you enable igbinary extension, then automatic use Driver\IgBinary. Or you can define own implmentation by Driver interface.

## Example why use Serialize class
In many use cases is igbinary faster. Anybody use in third party library h4kuna\Serialize\Serialize::encode/decode. If you enable igbinary and for this third party case you want to disable and to use standard serialization. See example.

## Compatibility
External library in vendor
```php
namespace Com\Example;

You are using php serialize and you want to use igbinary. You can enable igbinaty on fly and old serialized data will be decoded by old php serialize.
class Foo implements \Serializable {
public function serialize(): ?string {
return Serialize::encode($this, __CLASS__);
}

> Support compatibility, if you have serialized data by php serialize, then you can decode by IgBinary::decode() and vice versa.
public function unserialize(string $data): void {
Serialize::decode($data, __CLASS__);
// do anything
}
}
```

Enable standard serialization for class above.

## Enable custom driver
Register you driver after autoload. By default, you can skip this step.
```php
require __DIR__ . '/vendor/autoload.php';
\h4kuna\Serialize\Serialize::setUp(Php::class);
use h4kuna\Serialize\Driver;
use h4kuna\Serialize\Serialize;

require_once __DIR__ . '/vendor/autoload.php';
Serialize::setUp(Driver\IgBinary::class, [
Com\Example\Foo::class => Driver\Php::class // only for Com\Example\Foo use case
]);
```

## Compatibility

You are using php serialize and you want to use igbinary. You can enable igbinaty on fly and old serialized data will be decoded by old php serialize.

> Support compatibility, if you have serialized data by php serialize, then you can decode by IgBinary::decode() and vice versa.
## Enable igbinary

1. Install igbinary extension
2.
```php
require __DIR__ . '/vendor/autoload.php';
\h4kuna\Serialize\Serialize::setUp(IgBinary::class);
```

Works!

Expand All @@ -35,7 +63,7 @@ Works!
1. set Php driver after vendor/autoload.php
```php
require __DIR__ . '/vendor/autoload.php';
\h4kuna\Serialize\Serialize::setUp(Php::class);
// \h4kuna\Serialize\Serialize::setUp(IgBinary::class); remove
```
2. wait if your all data will be decoded
3. uninstall igbinary extension
4 changes: 2 additions & 2 deletions src/Base64.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class Base64
/** @param mixed $value */
public static function encode($value): string
{
return base64_encode(Serialize::encode($value));
return base64_encode(Serialize::encode($value, __CLASS__));
}


Expand All @@ -24,7 +24,7 @@ public static function decode(string $value, bool $strict = true)
throw new InvalidStateException(sprintf('This is not valid base64 string. "%s"', $value));
}

return Serialize::decode($base);
return Serialize::decode($base, __CLASS__);
}

}
15 changes: 9 additions & 6 deletions src/Driver/SetUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@ final class SetUp implements Driver

public static function encode($value): string
{
self::boot();
return Serialize::encode($value);
return self::boot()::encode($value);
}

public static function decode(string $value)
{
self::boot();
return Serialize::decode($value);
return self::boot()::decode($value);
}

private static function boot(): void
/**
* @return class-string<Driver>
*/
private static function boot(): string
{
Serialize::setUp(Php::class);
$class = Php::class;
Serialize::setUp($class);
return $class;
}
}
38 changes: 31 additions & 7 deletions src/Serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
use h4kuna\Serialize\Exception\InvalidStateException;
use Nette\StaticClass;

final class Serialize implements Driver
/**
* How use caseId @see Base64
*/
final class Serialize
{
use StaticClass;

/** @var class-string<Driver> */
private static string $driver = SetUp::class;

/** @var array<non-empty-string, class-string<Driver>> */
private static array $cases = [];

/**
* @param class-string<Driver> $driver
* @param array<non-empty-string, class-string<Driver>> $cases
*/
public static function setUp(string $driver): void
public static function setUp(string $driver, array $cases = []): void
{
if (self::$driver !== SetUp::class) {
if (self::$driver === $driver) {
Expand All @@ -25,17 +32,34 @@ public static function setUp(string $driver): void
throw new InvalidStateException(sprintf('Driver was already set up "%s" and you want "%s".', self::$driver, $driver));
}
self::$driver = $driver;
self::$cases = $cases;
}


public static function encode($value): string
/**
* @param mixed $value
* @param non-empty-string $caseId - is for control what driver is used and programmer can change it per use case
*/
public static function encode($value, string $caseId): string
{
return (self::$driver)::encode($value);
return self::resolveDriver($caseId)::encode($value);
}


public static function decode(string $value)
/**
* @param non-empty-string $caseId - is for control what driver is used and programmer can change it per use case
*
* @return mixed
*/
public static function decode(string $value, string $caseId)
{
return self::resolveDriver($caseId)::decode($value);
}

/**
* @return class-string<Driver>
*/
private static function resolveDriver(string $caseId): string
{
return (self::$driver)::decode($value);
return self::$cases[$caseId] ?? self::$driver;
}
}
2 changes: 1 addition & 1 deletion tests/src/CheckDefaulDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

require_once __DIR__ . '/../bootstrap.php';

$data = Serialize::encode('foo');
$data = Serialize::encode('foo', 'h4kuna-serialize-test');
Assert::same('foo', Php::decode($data));
Serialize::setUp(Php::class); // does not throw exception
Assert::exception(fn () => Serialize::setUp(IgBinary::class), InvalidStateException::class, 'Driver was already set up "h4kuna\Serialize\Driver\Php" and you want "h4kuna\Serialize\Driver\IgBinary".');
2 changes: 1 addition & 1 deletion tests/src/IgBinaryForceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
require_once __DIR__ . '/../bootstrap.php';
Serialize::setUp(IgBinary::class);

$data = Serialize::encode('foo');
$data = Serialize::encode('foo', 'h4kuna-serialize-test');
Assert::same('foo', IgBinary::decode($data));

0 comments on commit 5a322a3

Please sign in to comment.