Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ composer require patchlevel/hydrator
To use the hydrator you just have to create an instance of it.

```php
use Patchlevel\Hydrator\MetadataHydrator;
use Patchlevel\Hydrator\StackHydrator;

$hydrator = MetadataHydrator::create();
$hydrator = StackHydrator::create();
```

After that you can hydrate any classes or objects. Also `final`, `readonly` classes with `property promotion`.
Expand Down Expand Up @@ -483,9 +483,9 @@ class NameGuesser implements Guesser
To use this Guesser, you must specify it when creating the Hydrator:

```php
use Patchlevel\Hydrator\MetadataHydrator;
use Patchlevel\Hydrator\StackHydrator;

$hydrator = MetadataHydrator::create([new NameGuesser()]);
$hydrator = StackHydrator::create([new NameGuesser()]);
```

> [!NOTE]
Expand Down Expand Up @@ -595,7 +595,7 @@ For this functionality we use the [symfony/event-dispatcher](https://symfony.com
use Patchlevel\Hydrator\Cryptography\SensitiveDataPayloadCryptographer;
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyStore;
use Patchlevel\Hydrator\Metadata\Event\EventMetadataFactory;
use Patchlevel\Hydrator\MetadataHydrator;
use Patchlevel\Hydrator\StackHydrator;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Patchlevel\Hydrator\Event\PostExtract;
use Patchlevel\Hydrator\Event\PreHydrate;
Expand All @@ -616,7 +616,7 @@ $eventDispatcher->addListener(
}
);

$hydrator = new MetadataHydrator(eventDispatcher: $eventDispatcher);
$hydrator = new StackHydrator(eventDispatcher: $eventDispatcher);
```

### Cryptography
Expand Down Expand Up @@ -742,11 +742,11 @@ Here we show you how to configure the cryptography.
use Patchlevel\Hydrator\Cryptography\SensitiveDataPayloadCryptographer;
use Patchlevel\Hydrator\Cryptography\Store\CipherKeyStore;
use Patchlevel\Hydrator\Metadata\Event\EventMetadataFactory;
use Patchlevel\Hydrator\MetadataHydrator;
use Patchlevel\Hydrator\StackHydrator;

$cipherKeyStore = new InMemoryCipherKeyStore();
$cryptographer = SensitiveDataPayloadCryptographer::createWithDefaultSettings($cipherKeyStore);
$hydrator = new MetadataHydrator(cryptographer: $cryptographer);
$hydrator = new StackHydrator(cryptographer: $cryptographer);
```

> [!WARNING]
Expand Down
24 changes: 12 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ parameters:
count: 1
path: src/Metadata/ClassMetadata.php

-
message: '#^Parameter \#2 \$data of method Patchlevel\\Hydrator\\Middleware\\Middleware\:\:hydrate\(\) expects array\<string, mixed\>, array\<mixed, mixed\> given\.$#'
identifier: argument.type
count: 3
path: src/MetadataHydrator.php

-
message: '#^Property Patchlevel\\Hydrator\\Normalizer\\EnumNormalizer\:\:\$enum \(class\-string\<BackedEnum\>\|null\) does not accept string\.$#'
identifier: assign.propertyType
Expand All @@ -90,6 +84,12 @@ parameters:
count: 1
path: src/Normalizer/ObjectNormalizer.php

-
message: '#^Parameter \#2 \$data of method Patchlevel\\Hydrator\\Middleware\\Middleware\:\:hydrate\(\) expects array\<string, mixed\>, array\<mixed, mixed\> given\.$#'
identifier: argument.type
count: 3
path: src/StackHydrator.php

-
message: '#^Property Patchlevel\\Hydrator\\Tests\\Unit\\Extension\\Cryptography\\Fixture\\ChildWithSensitiveDataWithIdentifierDto\:\:\$email is never read, only written\.$#'
identifier: property.onlyWritten
Expand All @@ -108,12 +108,6 @@ parameters:
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

-
message: '#^Parameter \#1 \$class of method Patchlevel\\Hydrator\\MetadataHydrator\:\:hydrate\(\) expects class\-string\<Unknown\>, string given\.$#'
identifier: argument.type
count: 1
path: tests/Unit/MetadataHydratorTest.php

-
message: '#^Cannot cast mixed to int\.$#'
identifier: cast.int
Expand All @@ -137,3 +131,9 @@ parameters:
identifier: cast.string
count: 2
path: tests/Unit/Normalizer/ArrayShapeNormalizerTest.php

-
message: '#^Parameter \#1 \$class of method Patchlevel\\Hydrator\\StackHydrator\:\:hydrate\(\) expects class\-string\<Unknown\>, string given\.$#'
identifier: argument.type
count: 1
path: tests/Unit/StackHydratorTest.php
2 changes: 1 addition & 1 deletion src/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class CoreExtension implements Extension
{
public function configure(HydratorBuilder $builder): void
public function configure(StackHydratorBuilder $builder): void
{
$builder->addMiddleware(new TransformMiddleware(), -64);
$builder->addGuesser(new BuiltInGuesser(), -64);
Expand Down
2 changes: 1 addition & 1 deletion src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface Extension
{
public function configure(HydratorBuilder $builder): void;
public function configure(StackHydratorBuilder $builder): void;
}
4 changes: 2 additions & 2 deletions src/Extension/Cryptography/CryptographyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Patchlevel\Hydrator\Extension\Cryptography;

use Patchlevel\Hydrator\Extension;
use Patchlevel\Hydrator\HydratorBuilder;
use Patchlevel\Hydrator\StackHydratorBuilder;

final class CryptographyExtension implements Extension
{
Expand All @@ -14,7 +14,7 @@ public function __construct(
) {
}

public function configure(HydratorBuilder $builder): void
public function configure(StackHydratorBuilder $builder): void
{
$builder->addMetadataEnricher(new CryptographyMetadataEnricher(), 64);
$builder->addMiddleware(new CryptographyMiddleware($this->cryptography), 64);
Expand Down
4 changes: 2 additions & 2 deletions src/Extension/Lifecycle/LifecycleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace Patchlevel\Hydrator\Extension\Lifecycle;

use Patchlevel\Hydrator\Extension;
use Patchlevel\Hydrator\HydratorBuilder;
use Patchlevel\Hydrator\StackHydratorBuilder;

final readonly class LifecycleExtension implements Extension
{
public function configure(HydratorBuilder $builder): void
public function configure(StackHydratorBuilder $builder): void
{
$builder->addMiddleware(new LifecycleMiddleware());
$builder->addMetadataEnricher(new LifecycleMetadataEnricher());
Expand Down
2 changes: 1 addition & 1 deletion src/MetadataHydrator.php → src/StackHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use const PHP_VERSION_ID;

final class MetadataHydrator implements Hydrator
final class StackHydrator implements Hydrator
{
/** @var array<class-string, ClassMetadata> */
private array $classMetadata = [];
Expand Down
6 changes: 3 additions & 3 deletions src/HydratorBuilder.php → src/StackHydratorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use function array_merge;
use function krsort;

final class HydratorBuilder
final class StackHydratorBuilder
{
private bool $defaultLazy = false;

Expand Down Expand Up @@ -78,7 +78,7 @@ public function setCache(CacheItemPoolInterface|CacheInterface|null $cache): sta
return $this;
}

public function build(): Hydrator
public function build(): StackHydrator
{
krsort($this->guessers);
krsort($this->metadataEnrichers);
Expand All @@ -99,7 +99,7 @@ public function build(): Hydrator
$metadataFactory = new Psr16MetadataFactory($metadataFactory, $this->cache);
}

return new MetadataHydrator(
return new StackHydrator(
$metadataFactory,
array_merge(...$this->middlewares),
$this->defaultLazy,
Expand Down
4 changes: 2 additions & 2 deletions tests/Benchmark/HydratorBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Patchlevel\Hydrator\CoreExtension;
use Patchlevel\Hydrator\Hydrator;
use Patchlevel\Hydrator\HydratorBuilder;
use Patchlevel\Hydrator\StackHydratorBuilder;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\ProfileCreated;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\ProfileId;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\Skill;
Expand All @@ -19,7 +19,7 @@ final class HydratorBench

public function __construct()
{
$this->hydrator = (new HydratorBuilder())
$this->hydrator = (new StackHydratorBuilder())
->useExtension(new CoreExtension())
->build();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Benchmark/HydratorWithCryptographyBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Patchlevel\Hydrator\Extension\Cryptography\CryptographyExtension;
use Patchlevel\Hydrator\Extension\Cryptography\Store\InMemoryCipherKeyStore;
use Patchlevel\Hydrator\Hydrator;
use Patchlevel\Hydrator\HydratorBuilder;
use Patchlevel\Hydrator\StackHydratorBuilder;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\ProfileCreated;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\ProfileId;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\Skill;
Expand All @@ -26,7 +26,7 @@ public function __construct()
{
$this->store = new InMemoryCipherKeyStore();

$this->hydrator = (new HydratorBuilder())
$this->hydrator = (new StackHydratorBuilder())
->useExtension(new CoreExtension())
->useExtension(new CryptographyExtension(BaseCryptographer::createWithOpenssl($this->store)))
->build();
Expand Down
4 changes: 2 additions & 2 deletions tests/Benchmark/HydratorWithLazyBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Patchlevel\Hydrator\CoreExtension;
use Patchlevel\Hydrator\Hydrator;
use Patchlevel\Hydrator\HydratorBuilder;
use Patchlevel\Hydrator\StackHydratorBuilder;
use Patchlevel\Hydrator\Tests\Benchmark\Fixture\ProfileCreated;
use PhpBench\Attributes as Bench;

Expand All @@ -17,7 +17,7 @@ final class HydratorWithLazyBench

public function __construct()
{
$this->hydrator = (new HydratorBuilder())
$this->hydrator = (new StackHydratorBuilder())
->useExtension(new CoreExtension())
->enableDefaultLazy()
->build();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Extension/Lifecycle/LifecycleExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Patchlevel\Hydrator\CoreExtension;
use Patchlevel\Hydrator\Extension\Lifecycle\LifecycleExtension;
use Patchlevel\Hydrator\HydratorBuilder;
use Patchlevel\Hydrator\StackHydratorBuilder;
use Patchlevel\Hydrator\Tests\Unit\Fixture\LifecycleFixture;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand All @@ -16,7 +16,7 @@ final class LifecycleExtensionTest extends TestCase
{
public function testIntegration(): void
{
$hydrator = (new HydratorBuilder())
$hydrator = (new StackHydratorBuilder())
->useExtension(new CoreExtension())
->useExtension(new LifecycleExtension())
->build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,35 @@
use Patchlevel\Hydrator\Extension;
use Patchlevel\Hydrator\Guesser\ChainGuesser;
use Patchlevel\Hydrator\Guesser\Guesser;
use Patchlevel\Hydrator\HydratorBuilder;
use Patchlevel\Hydrator\Metadata\AttributeMetadataFactory;
use Patchlevel\Hydrator\Metadata\EnrichingMetadataFactory;
use Patchlevel\Hydrator\Metadata\MetadataEnricher;
use Patchlevel\Hydrator\Metadata\Psr16MetadataFactory;
use Patchlevel\Hydrator\Metadata\Psr6MetadataFactory;
use Patchlevel\Hydrator\MetadataHydrator;
use Patchlevel\Hydrator\Middleware\Middleware;
use Patchlevel\Hydrator\StackHydrator;
use Patchlevel\Hydrator\StackHydratorBuilder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
use ReflectionProperty;

#[CoversClass(HydratorBuilder::class)]
final class HydratorBuilderTest extends TestCase
#[CoversClass(StackHydratorBuilder::class)]
final class StackHydratorBuilderTest extends TestCase
{
public function testBuild(): void
{
$hydrator = (new HydratorBuilder())->build();

self::assertInstanceOf(MetadataHydrator::class, $hydrator);
}

public function testAddMiddlewareWithPriority(): void
{
$middleware1 = $this->createMock(Middleware::class);
$middleware2 = $this->createMock(Middleware::class);

$builder = new HydratorBuilder();
$builder = new StackHydratorBuilder();
$builder->addMiddleware($middleware1, 10);
$builder->addMiddleware($middleware2, 20);

$hydrator = $builder->build();

$reflection = new ReflectionProperty(MetadataHydrator::class, 'middlewares');
$reflection = new ReflectionProperty(StackHydrator::class, 'middlewares');
$middlewares = $reflection->getValue($hydrator);

self::assertSame([$middleware2, $middleware1], $middlewares);
Expand All @@ -53,13 +46,13 @@ public function testAddMetadataEnricherWithPriority(): void
$enricher1 = $this->createMock(MetadataEnricher::class);
$enricher2 = $this->createMock(MetadataEnricher::class);

$builder = new HydratorBuilder();
$builder = new StackHydratorBuilder();
$builder->addMetadataEnricher($enricher1, 10);
$builder->addMetadataEnricher($enricher2, 20);

$hydrator = $builder->build();

$reflection = new ReflectionProperty(MetadataHydrator::class, 'metadataFactory');
$reflection = new ReflectionProperty(StackHydrator::class, 'metadataFactory');
$enrichingMetadataFactory = $reflection->getValue($hydrator);

self::assertInstanceOf(EnrichingMetadataFactory::class, $enrichingMetadataFactory);
Expand All @@ -75,13 +68,13 @@ public function testAddGuesserWithPriority(): void
$guesser1 = $this->createMock(Guesser::class);
$guesser2 = $this->createMock(Guesser::class);

$builder = new HydratorBuilder();
$builder = new StackHydratorBuilder();
$builder->addGuesser($guesser1, 10);
$builder->addGuesser($guesser2, 20);

$hydrator = $builder->build();

$reflection = new ReflectionProperty(MetadataHydrator::class, 'metadataFactory');
$reflection = new ReflectionProperty(StackHydrator::class, 'metadataFactory');
$enrichingMetadataFactory = $reflection->getValue($hydrator);

self::assertInstanceOf(EnrichingMetadataFactory::class, $enrichingMetadataFactory);
Expand All @@ -104,19 +97,19 @@ public function testAddGuesserWithPriority(): void

public function testEnableDefaultLazy(): void
{
$builder = new HydratorBuilder();
$builder = new StackHydratorBuilder();
$builder->enableDefaultLazy();

$hydrator = $builder->build();

$reflection = new ReflectionProperty(MetadataHydrator::class, 'defaultLazy');
$reflection = new ReflectionProperty(StackHydrator::class, 'defaultLazy');
self::assertTrue($reflection->getValue($hydrator));
}

public function testUseExtension(): void
{
$extension = $this->createMock(Extension::class);
$builder = new HydratorBuilder();
$builder = new StackHydratorBuilder();

$extension->expects(self::once())
->method('configure')
Expand All @@ -129,12 +122,12 @@ public function testCachePsr6(): void
{
$cache = $this->createMock(CacheItemPoolInterface::class);

$builder = new HydratorBuilder();
$builder = new StackHydratorBuilder();
$builder->setCache($cache);

$hydrator = $builder->build();

$reflection = new ReflectionProperty(MetadataHydrator::class, 'metadataFactory');
$reflection = new ReflectionProperty(StackHydrator::class, 'metadataFactory');
$factory = $reflection->getValue($hydrator);

self::assertInstanceOf(Psr6MetadataFactory::class, $factory);
Expand All @@ -144,12 +137,12 @@ public function testCachePsr16(): void
{
$cache = $this->createMock(CacheInterface::class);

$builder = new HydratorBuilder();
$builder = new StackHydratorBuilder();
$builder->setCache($cache);

$hydrator = $builder->build();

$reflection = new ReflectionProperty(MetadataHydrator::class, 'metadataFactory');
$reflection = new ReflectionProperty(StackHydrator::class, 'metadataFactory');
$factory = $reflection->getValue($hydrator);

self::assertInstanceOf(Psr16MetadataFactory::class, $factory);
Expand Down
Loading
Loading