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
10 changes: 8 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
parameters:
ignoreErrors:
-
message: '#^Method Patchlevel\\ODM\\Hydrator\\DocumentHydrator\:\:__construct\(\) has parameter \$documentMetadata with generic class Patchlevel\\ODM\\Metadata\\DocumentMetadata but does not specify its types\: T$#'
identifier: missingType.generics
count: 1
path: src/Hydrator/DocumentHydrator.php

-
message: '#^Parameter \#1 \$data of method Patchlevel\\ODM\\Hydrator\\MongoDBCipherKeyStore\:\:hydrate\(\) expects array\{_id\: string, subject_id\: string, key\: non\-empty\-string, method\: non\-empty\-string, created_at\: string\}, array\|object given\.$#'
identifier: argument.type
Expand Down Expand Up @@ -73,13 +79,13 @@ parameters:
path: src/Repository/MongoDBRepository.php

-
message: '#^Parameter \#2 \$data of method Patchlevel\\Hydrator\\HydratorWithContext\:\:hydrate\(\) expects array\<string, mixed\>, array\|object given\.$#'
message: '#^Parameter \#2 \$data of method Patchlevel\\ODM\\Hydrator\\DocumentHydrator\:\:hydrate\(\) expects array\<string, mixed\>, array\|object given\.$#'
identifier: argument.type
count: 2
path: src/Repository/MongoDBRepository.php

-
message: '#^Parameter \#2 \$data of method Patchlevel\\Hydrator\\HydratorWithContext\:\:hydrate\(\) expects array\<string, mixed\>, array\|object\|null given\.$#'
message: '#^Parameter \#2 \$data of method Patchlevel\\ODM\\Hydrator\\DocumentHydrator\:\:hydrate\(\) expects array\<string, mixed\>, array\|object\|null given\.$#'
identifier: argument.type
count: 2
path: src/Repository/MongoDBRepository.php
Expand Down
61 changes: 61 additions & 0 deletions src/Hydrator/DocumentHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Patchlevel\ODM\Hydrator;

use Patchlevel\Hydrator\ClassNotSupported;
use Patchlevel\Hydrator\HydratorWithContext;
use Patchlevel\ODM\Metadata\DocumentMetadata;

final class DocumentHydrator implements HydratorWithContext
{
private const ID_FIELD_NAME = '_id';

private string|null $fieldNameOverride;

public function __construct(
private readonly HydratorWithContext $hydrator,
private readonly DocumentMetadata $documentMetadata,
) {
$this->fieldNameOverride = $this->documentMetadata->fields[$this->documentMetadata->idProperty]->fieldNameOverride;
}

/**
* @param class-string<T> $class
* @param array<string, mixed> $data
* @param array<string, mixed> $context
*
* @return T
*
* @throws ClassNotSupported if the class is not supported or not found.
*
* @template T of object
*/
public function hydrate(string $class, array $data, array $context = []): object
{
if ($this->fieldNameOverride) {
$data[$this->fieldNameOverride] = $data[self::ID_FIELD_NAME];
unset($data[self::ID_FIELD_NAME]);
}

return $this->hydrator->hydrate($class, $data, $context);
}

/**
* @param array<string, mixed> $context
*
* @return array<string, mixed>
*/
public function extract(object $object, array $context = []): array
{
$data = $this->hydrator->extract($object, $context);

if ($this->fieldNameOverride) {
$data[self::ID_FIELD_NAME] = $data[$this->fieldNameOverride];
unset($data[$this->fieldNameOverride]);
}

return $data;
}
}
16 changes: 0 additions & 16 deletions src/Hydrator/ODMExtension.php

This file was deleted.

71 changes: 0 additions & 71 deletions src/Hydrator/ODMMiddleware.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/Metadata/AttributeDocumentMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public function metadata(string $className): DocumentMetadata
$idProperty = $this->getIdProperty($reflection);

foreach ($reflection->getProperties() as $reflectionProperty) {
$field = $this->fieldResolver?->resolve($reflectionProperty);

if ($idProperty === $reflectionProperty->getName()) {
$fields[$reflectionProperty->getName()] = new FieldMapping('_id');
$fields[$reflectionProperty->getName()] = new FieldMapping('_id', [], $field?->fieldName);

continue;
}

$field = $this->fieldResolver?->resolve($reflectionProperty);

if (!$field) {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/FieldMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
public function __construct(
public string $fieldName,
public array $children = [],
public string|null $fieldNameOverride = null,
) {
}
}
42 changes: 12 additions & 30 deletions src/Repository/MongoDBRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MongoDB\Database;
use MongoDB\Driver\Exception\ServerException;
use Patchlevel\Hydrator\HydratorWithContext;
use Patchlevel\ODM\Hydrator\DocumentHydrator;
use Patchlevel\ODM\Metadata\DocumentMetadata;

use function array_map;
Expand All @@ -24,12 +25,15 @@
{
private Collection $collection;

private DocumentHydrator $hydrator;

/** @param DocumentMetadata<T> $metadata */
public function __construct(
private Database $database,
private DocumentMetadata $metadata,
private HydratorWithContext $hydrator,
HydratorWithContext $hydrator,
) {
$this->hydrator = new DocumentHydrator($hydrator, $metadata);
$this->collection = $this->database->selectCollection($this->metadata->collection);
}

Expand All @@ -44,10 +48,7 @@ public function insert(object ...$objects): void
throw new WrongClass($this->metadata->className, $object::class);
}

$data = $this->hydrator->extract(
$object,
[DocumentMetadata::class => $this->metadata],
);
$data = $this->hydrator->extract($object);
$this->collection->insertOne($data);

return;
Expand All @@ -58,10 +59,7 @@ public function insert(object ...$objects): void
throw new WrongClass($this->metadata->className, $object::class);
}

return $this->hydrator->extract(
$object,
[DocumentMetadata::class => $this->metadata],
);
return $this->hydrator->extract($object);
}, $objects));
} catch (ServerException $e) {
throw new InsertionFailed($e->getMessage(), $e->getCode(), $e);
Expand All @@ -82,10 +80,7 @@ public function update(object ...$objects): void
throw new WrongClass($this->metadata->className, $object::class);
}

$data = $this->hydrator->extract(
$object,
[DocumentMetadata::class => $this->metadata],
);
$data = $this->hydrator->extract($object);

$this->collection->updateOne(['_id' => $data['_id']], ['$set' => $data]);

Expand All @@ -98,7 +93,7 @@ function (object $object): array {
throw new WrongClass($this->metadata->className, $object::class);
}

$data = $this->hydrator->extract($object, [DocumentMetadata::class => $this->metadata]);
$data = $this->hydrator->extract($object);

return [
'updateOne' => [
Expand Down Expand Up @@ -143,11 +138,7 @@ public function find(string $id): object|null
return null;
}

return $this->hydrator->hydrate(
$this->metadata->className,
$data,
[DocumentMetadata::class => $this->metadata],
);
return $this->hydrator->hydrate($this->metadata->className, $data);
}

public function remove(string ...$id): void
Expand All @@ -169,11 +160,7 @@ public function findAll(): iterable
]);

foreach ($cursor as $document) {
yield $this->hydrator->hydrate(
$this->metadata->className,
$document,
[DocumentMetadata::class => $this->metadata],
);
yield $this->hydrator->hydrate($this->metadata->className, $document);
}
}

Expand Down Expand Up @@ -211,7 +198,6 @@ public function findBy(
yield $this->hydrator->hydrate(
$this->metadata->className,
$document,
[DocumentMetadata::class => $this->metadata],
);
}
}
Expand Down Expand Up @@ -239,11 +225,7 @@ public function findOneBy(array $filter = [], array|null $orderBy = null): objec
return null;
}

return $this->hydrator->hydrate(
$this->metadata->className,
$data,
[DocumentMetadata::class => $this->metadata],
);
return $this->hydrator->hydrate($this->metadata->className, $data);
}

public function count(): int
Expand Down
22 changes: 5 additions & 17 deletions src/Repository/MongoDBRepositoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
namespace Patchlevel\ODM\Repository;

use MongoDB\Client;
use Patchlevel\Hydrator\CoreExtension;
use Patchlevel\Hydrator\Extension;
use Patchlevel\Hydrator\HydratorWithContext;
use Patchlevel\Hydrator\StackHydratorBuilder;
use Patchlevel\ODM\Hydrator\ODMExtension;
use Patchlevel\Hydrator\StackHydrator;
use Patchlevel\ODM\Metadata\AttributeDocumentMetadataFactory;
use Patchlevel\ODM\Metadata\DocumentMetadataFactory;
use Patchlevel\ODM\Metadata\StackHydratorFieldMappingResolver;
Expand Down Expand Up @@ -51,19 +48,10 @@ public function get(string $documentClass): MongoDBRepository
return $this->repositories[$documentClass];
}

/** @param list<Extension> $extensions */
public static function create(Client $client, array $extensions = []): self
{
$builder = (new StackHydratorBuilder())
->useExtension(new CoreExtension())
->useExtension(new ODMExtension());

foreach ($extensions as $extension) {
$builder->useExtension($extension);
}

$hydrator = $builder->build();

public static function create(
Client $client,
StackHydrator $hydrator = new StackHydrator(),
): self {
$metadataFactory = new AttributeDocumentMetadataFactory(
new StackHydratorFieldMappingResolver($hydrator),
);
Expand Down
Loading
Loading