Skip to content

Commit

Permalink
Merge pull request #41 from boesing/deprecate/serializer
Browse files Browse the repository at this point in the history
Deprecate `Laminas\Serializer\Serializer`
  • Loading branch information
boesing authored Sep 4, 2023
2 parents 1b8b54a + 344a2b9 commit 0aba33d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Laminas\Serializer;

use Laminas\Serializer\Adapter\AdapterInterface;
use Laminas\Serializer\Adapter\PhpSerialize;

class ConfigProvider
{
/**
Expand All @@ -34,6 +37,7 @@ public function getDependencyConfig()
'aliases' => [],
'factories' => [
'SerializerAdapterManager' => AdapterPluginManagerFactory::class,
AdapterInterface::class => new GenericSerializerFactory(PhpSerialize::class),
],
];
}
Expand Down
26 changes: 26 additions & 0 deletions src/GenericSerializerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Laminas\Serializer;

use Laminas\Serializer\Adapter\AdapterInterface;
use Psr\Container\ContainerInterface;

final class GenericSerializerFactory
{
/**
* @param class-string<AdapterInterface> $serializerName
* @param array<string,mixed>|null $options
*/
public function __construct(private string $serializerName, private array|null $options = null)
{
}

public function __invoke(ContainerInterface $container): AdapterInterface
{
$plugins = $container->get(AdapterPluginManager::class);

return $plugins->build($this->serializerName, $this->options);
}
}
3 changes: 3 additions & 0 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Laminas\ServiceManager\ServiceManager;
use Traversable;

/**
* @deprecated This class will be removed with v3.0.0. Please migrate your code according to the migration guide.
*/
// phpcs:ignore
abstract class Serializer
{
Expand Down
34 changes: 34 additions & 0 deletions test/GenericSerializerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Serializer;

use Laminas\Serializer\Adapter\Json;
use Laminas\Serializer\Adapter\JsonOptions;
use Laminas\Serializer\AdapterPluginManager;
use Laminas\Serializer\GenericSerializerFactory;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

final class GenericSerializerFactoryTest extends TestCase
{
public function testWillRequestInstanceFromPluginManager(): void
{
$factory = new GenericSerializerFactory(Json::class, ['cycle_check' => true]);
$container = $this->createMock(ContainerInterface::class);
$plugins = new AdapterPluginManager($container);

$container
->expects(self::once())
->method('get')
->with(AdapterPluginManager::class)
->willReturn($plugins);

$adapter = $factory($container);
self::assertInstanceOf(Json::class, $adapter);
self::assertTrue($adapter->getOptions()->getCycleCheck());
// Verify that default of json options is false so that we do not accidentally test for default `true` value
self::assertFalse((new JsonOptions())->getCycleCheck());
}
}

0 comments on commit 0aba33d

Please sign in to comment.