17
17
18
18
use FastyBird \JsonApi \JsonApi ;
19
19
use IPub \JsonAPIDocument ;
20
+ use Nette \DI ;
20
21
use SplObjectStorage ;
21
22
22
23
/**
@@ -31,13 +32,16 @@ class HydratorsContainer
31
32
/** @var SplObjectStorage<Hydrator, null> */
32
33
private SplObjectStorage $ hydrators ;
33
34
34
- /** @var JsonApi\JsonApiSchemaContainer */
35
- private JsonApi \JsonApiSchemaContainer $ jsonApiSchemaContainer ;
35
+ /** @var DI\Container */
36
+ private DI \Container $ container ;
37
+
38
+ /** @var JsonApi\JsonApiSchemaContainer|null */
39
+ private ?JsonApi \JsonApiSchemaContainer $ jsonApiSchemaContainer = null ;
36
40
37
41
public function __construct (
38
- JsonApi \ JsonApiSchemaContainer $ jsonApiSchemaContainer
42
+ DI \ Container $ container
39
43
) {
40
- $ this ->jsonApiSchemaContainer = $ jsonApiSchemaContainer ;
44
+ $ this ->container = $ container ;
41
45
42
46
$ this ->hydrators = new SplObjectStorage ();
43
47
}
@@ -54,7 +58,7 @@ public function findHydrator(JsonAPIDocument\IDocument $document): ?Hydrator
54
58
$ this ->hydrators ->rewind ();
55
59
56
60
foreach ($ this ->hydrators as $ hydrator ) {
57
- $ schema = $ this ->jsonApiSchemaContainer ->getSchemaByClassName ($ hydrator ->getEntityName ());
61
+ $ schema = $ this ->getSchemaContainer () ->getSchemaByClassName ($ hydrator ->getEntityName ());
58
62
59
63
if ($ schema ->getType () === $ document ->getResource ()->getType ()) {
60
64
return $ hydrator ;
@@ -78,4 +82,18 @@ public function add(Hydrator $hydrator): void
78
82
}
79
83
}
80
84
85
+ /**
86
+ * @return JsonApi\JsonApiSchemaContainer
87
+ */
88
+ private function getSchemaContainer (): JsonApi \JsonApiSchemaContainer
89
+ {
90
+ if ($ this ->jsonApiSchemaContainer !== null ) {
91
+ return $ this ->jsonApiSchemaContainer ;
92
+ }
93
+
94
+ $ this ->jsonApiSchemaContainer = $ this ->container ->getByType (JsonApi \JsonApiSchemaContainer::class);
95
+
96
+ return $ this ->jsonApiSchemaContainer ;
97
+ }
98
+
81
99
}
0 commit comments