Skip to content

Commit 5c5e811

Browse files
committed
Creating hydrator container
1 parent 03b4802 commit 5c5e811

File tree

5 files changed

+135
-15
lines changed

5 files changed

+135
-15
lines changed

phpstan.neon

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
parameters:
2-
level: max
3-
stubFiles:
4-
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Factories/FactoryInterface.php
5-
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/ContextInterface.php
6-
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/LinkInterface.php
7-
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/SchemaContainerInterface.php
8-
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/SchemaInterface.php
9-
- tests/PHPStan/stubs/Neomerx/JsonApi/Factories/Factory.php
2+
level: max
3+
stubFiles:
4+
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Factories/FactoryInterface.php
5+
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/ContextInterface.php
6+
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/LinkInterface.php
7+
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/SchemaContainerInterface.php
8+
- tests/PHPStan/stubs/Neomerx/JsonApi/Contracts/Schema/SchemaInterface.php
9+
- tests/PHPStan/stubs/Neomerx/JsonApi/Factories/Factory.php
10+
checkGenericClassInNonGenericObjectType: false

src/DI/JsonApiExtension.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use FastyBird\JsonApi\Builder;
1919
use FastyBird\JsonApi\Helpers;
20+
use FastyBird\JsonApi\Hydrators;
2021
use FastyBird\JsonApi\JsonApi;
2122
use FastyBird\JsonApi\Middleware;
2223
use FastyBird\JsonApi\Schemas;
@@ -96,6 +97,9 @@ public function loadConfiguration(): void
9697
],
9798
]);
9899

100+
$builder->addDefinition($this->prefix('hydrators.container'), new DI\Definitions\ServiceDefinition())
101+
->setType(Hydrators\HydratorsContainer::class);
102+
99103
$builder->addDefinition($this->prefix('schemas.container'), new DI\Definitions\ServiceDefinition())
100104
->setType(JsonApi\JsonApiSchemaContainer::class);
101105

@@ -130,6 +134,23 @@ public function beforeCompile(): void
130134
$schemaContainerService->addSetup('add', [$schemasService]);
131135
}
132136
}
137+
138+
/**
139+
* JSON:API HYDRATORS
140+
*/
141+
142+
$hydratorContainerServiceName = $builder->getByType(Hydrators\HydratorsContainer::class, true);
143+
144+
if ($hydratorContainerServiceName !== null) {
145+
$hydratorContainerService = $builder->getDefinition($hydratorContainerServiceName);
146+
assert($hydratorContainerService instanceof DI\Definitions\ServiceDefinition);
147+
148+
$hydratorsServices = $builder->findByType(Hydrators\Hydrator::class);
149+
150+
foreach ($hydratorsServices as $hydratorService) {
151+
$hydratorContainerService->addSetup('add', [$hydratorService]);
152+
}
153+
}
133154
}
134155

135156
}

src/Hydrators/Hydrator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ public function hydrate(
241241
return $result;
242242
}
243243

244+
/**
245+
* @return string
246+
*
247+
* @phpstan-return class-string
248+
*/
249+
abstract public function getEntityName(): string;
250+
244251
/**
245252
* @param string $entityClassName
246253
*
@@ -685,13 +692,6 @@ private function parseAnnotation(ReflectionProperty $rp, string $name): ?string
685692
return null;
686693
}
687694

688-
/**
689-
* @return string
690-
*
691-
* @phpstan-return class-string
692-
*/
693-
abstract protected function getEntityName(): string;
694-
695695
/**
696696
* @param string $className
697697
* @param JsonAPIDocument\Objects\IStandardObject<string, mixed> $attributes
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* HydratorsContainer.php
5+
*
6+
* @license More in LICENSE.md
7+
* @copyright https://www.fastybird.com
8+
* @author Adam Kadlec <[email protected]>
9+
* @package FastyBird:JsonApi!
10+
* @subpackage Hydrators
11+
* @since 0.7.0
12+
*
13+
* @date 11.01.22
14+
*/
15+
16+
namespace FastyBird\JsonApi\Hydrators;
17+
18+
use FastyBird\JsonApi\JsonApi;
19+
use IPub\JsonAPIDocument;
20+
use SplObjectStorage;
21+
22+
/**
23+
* API hydrators container
24+
*
25+
* @package FastyBird:JsonApi!
26+
* @subpackage Hydrators
27+
*/
28+
class HydratorsContainer
29+
{
30+
31+
/** @var SplObjectStorage<Hydrator, null> */
32+
private SplObjectStorage $hydrators;
33+
34+
/** @var JsonApi\JsonApiSchemaContainer */
35+
private JsonApi\JsonApiSchemaContainer $jsonApiSchemaContainer;
36+
37+
public function __construct(
38+
JsonApi\JsonApiSchemaContainer $jsonApiSchemaContainer
39+
) {
40+
$this->jsonApiSchemaContainer = $jsonApiSchemaContainer;
41+
42+
$this->hydrators = new SplObjectStorage();
43+
}
44+
45+
/**
46+
* @param JsonAPIDocument\IDocument $document
47+
*
48+
* @return Hydrator|null
49+
*
50+
* @phpstan-return Hydrator|null
51+
*/
52+
public function findHydrator(JsonAPIDocument\IDocument $document): ?Hydrator
53+
{
54+
$this->hydrators->rewind();
55+
56+
foreach ($this->hydrators as $hydrator) {
57+
$schema = $this->jsonApiSchemaContainer->getSchemaByClassName($hydrator->getEntityName());
58+
59+
if ($schema->getType() === $document->getResource()->getType()) {
60+
return $hydrator;
61+
}
62+
}
63+
64+
return null;
65+
}
66+
67+
/**
68+
* @param Hydrator $hydrator
69+
*
70+
* @return void
71+
*
72+
* @phpstan-param Hydrator $hydrator
73+
*/
74+
public function add(Hydrator $hydrator): void
75+
{
76+
if (!$this->hydrators->contains($hydrator)) {
77+
$this->hydrators->attach($hydrator);
78+
}
79+
}
80+
81+
}

src/JsonApi/JsonApiSchemaContainer.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace FastyBird\JsonApi\JsonApi;
1717

18+
use FastyBird\JsonApi\Exceptions;
1819
use FastyBird\JsonApi\Schemas;
1920
use Neomerx\JsonApi;
2021

@@ -52,6 +53,22 @@ public function add(Schemas\IJsonApiSchema $schema): void
5253
$this->setCreatedProvider($schema->getEntityClass(), $schema);
5354
}
5455

56+
/**
57+
* @param string $resourceType
58+
*
59+
* @return Schemas\IJsonApiSchema
60+
*/
61+
public function getSchemaByClassName(string $resourceType): Schemas\IJsonApiSchema
62+
{
63+
$schema = $this->getSchemaByType($resourceType);
64+
65+
if ($schema instanceof Schemas\IJsonApiSchema) {
66+
return $schema;
67+
}
68+
69+
throw new Exceptions\InvalidStateException('');
70+
}
71+
5572
/**
5673
* {@inheritDoc}
5774
*/

0 commit comments

Comments
 (0)