1 | <?php |
||
22 | class ResourceFactory implements ResourceFactoryContract |
||
23 | { |
||
24 | /** |
||
25 | * A service class, used to normalize data. |
||
26 | * |
||
27 | * @var \Flugg\Responder\Resources\DataNormalizer |
||
28 | */ |
||
29 | protected $normalizer; |
||
30 | |||
31 | /** |
||
32 | * A manager class, used to manage transformers. |
||
33 | * |
||
34 | * @var \Flugg\Responder\Contracts\Transformers\TransformerResolver |
||
35 | */ |
||
36 | protected $transformerResolver; |
||
37 | |||
38 | /** |
||
39 | * A resolver class, used to resolve resource keys. |
||
40 | * |
||
41 | * @var \Flugg\Responder\Contracts\Resources\ResourceKeyResolver |
||
42 | */ |
||
43 | protected $resourceKeyResolver; |
||
44 | |||
45 | /** |
||
46 | * Construct the factory class. |
||
47 | * |
||
48 | * @param \Flugg\Responder\Resources\DataNormalizer $normalizer |
||
49 | * @param \Flugg\Responder\Contracts\Transformers\TransformerResolver $transformerResolver |
||
50 | * @param \Flugg\Responder\Contracts\Resources\ResourceKeyResolver $resourceKeyResolver |
||
51 | */ |
||
52 | 6 | public function __construct(DataNormalizer $normalizer, TransformerResolver $transformerResolver, ResourceKeyResolver $resourceKeyResolver) |
|
58 | |||
59 | /** |
||
60 | * Make resource from the given data. |
||
61 | * |
||
62 | * @param mixed $data |
||
63 | * @param \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer |
||
64 | * @param string|null $resourceKey |
||
65 | * @return \League\Fractal\Resource\ResourceInterface |
||
66 | */ |
||
67 | 6 | public function make($data = null, $transformer = null, string $resourceKey = null): ResourceInterface |
|
81 | |||
82 | /** |
||
83 | * Resolve a transformer. |
||
84 | * |
||
85 | * @param mixed $data |
||
86 | * @param \Flugg\Responder\Transformers\Transformer|string|callable|null $transformer |
||
87 | * @return \Flugg\Responder\Transformers\Transformer|callable |
||
88 | */ |
||
89 | 5 | protected function resolveTransformer($data, $transformer) |
|
97 | |||
98 | /** |
||
99 | * Resolve a resource key. |
||
100 | * |
||
101 | * @param mixed $data |
||
102 | * @param string|null $resourceKey |
||
103 | * @return null|string |
||
104 | */ |
||
105 | 5 | protected function resolveResourceKey($data, string $resourceKey = null) |
|
109 | |||
110 | /** |
||
111 | * Instatiate a new resource instance. |
||
112 | * |
||
113 | * @param mixed $data |
||
114 | * @param \Flugg\Responder\Transformers\Transformer|callable|null $transformer |
||
115 | * @param string|null $resourceKey |
||
116 | * @return \League\Fractal\Resource\ResourceInterface |
||
117 | */ |
||
118 | 5 | protected function instatiateResource($data, $transformer = null, string $resourceKey = null): ResourceInterface |
|
128 | |||
129 | /** |
||
130 | * Indicates if the data belongs to a collection resource. |
||
131 | * |
||
132 | * @param mixed $data |
||
133 | * @return bool |
||
134 | */ |
||
135 | 4 | protected function shouldCreateCollection($data): bool |
|
143 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: