1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace W2w\Laravel\Apie\Plugins\Illuminate\ResourceFactories; |
5
|
|
|
|
6
|
|
|
use Illuminate\Container\Container; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use W2w\Lib\Apie\Exceptions\InvalidClassTypeException; |
9
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceFactoryInterface; |
10
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourcePersisterInterface; |
11
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceRetrieverInterface; |
12
|
|
|
|
13
|
|
|
class FromIlluminateContainerFactory implements ApiResourceFactoryInterface |
14
|
|
|
{ |
15
|
|
|
private $container; |
16
|
|
|
|
17
|
|
|
public function __construct(Container $container) |
18
|
|
|
{ |
19
|
|
|
$this->container = $container; |
20
|
|
|
} |
21
|
|
|
/** |
22
|
|
|
* Returns true if this factory can create this identifier. |
23
|
|
|
* |
24
|
|
|
* @param string $identifier |
25
|
|
|
* @return bool |
26
|
|
|
*/ |
27
|
|
|
public function hasApiResourceRetrieverInstance(string $identifier): bool |
28
|
|
|
{ |
29
|
|
|
return $this->validIdentifier($identifier); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Gets an instance of ApiResourceRetrieverInstance |
34
|
|
|
* @param string $identifier |
35
|
|
|
* @return ApiResourceRetrieverInterface |
36
|
|
|
*/ |
37
|
|
|
public function getApiResourceRetrieverInstance(string $identifier): ApiResourceRetrieverInterface |
38
|
|
|
{ |
39
|
|
|
$result = $this->container->make($identifier); |
40
|
|
|
if (!($result instanceof ApiResourceRetrieverInterface)) { |
41
|
|
|
throw new InvalidClassTypeException($identifier, 'ApiResourceRetrieverInterface'); |
42
|
|
|
} |
43
|
|
|
return $result; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns true if this factory can create this identifier. |
48
|
|
|
* |
49
|
|
|
* @param string $identifier |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
public function hasApiResourcePersisterInstance(string $identifier): bool |
53
|
|
|
{ |
54
|
|
|
return $this->validIdentifier($identifier); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets an instance of ApiResourceRetrieverInstance |
59
|
|
|
* @param string $identifier |
60
|
|
|
* @return ApiResourcePersisterInterface |
61
|
|
|
*/ |
62
|
|
|
public function getApiResourcePersisterInstance(string $identifier): ApiResourcePersisterInterface |
63
|
|
|
{ |
64
|
|
|
$result = $this->container->make($identifier); |
65
|
|
|
if (!($result instanceof ApiResourcePersisterInterface)) { |
66
|
|
|
throw new InvalidClassTypeException($identifier, 'ApiResourcePersisterInterface'); |
67
|
|
|
} |
68
|
|
|
return $result; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function validIdentifier(string $identifier): bool |
72
|
|
|
{ |
73
|
|
|
return $this->container->has($identifier) |
74
|
|
|
|| $this->container->bound($identifier) |
75
|
|
|
|| Str::startsWith($identifier, 'W2w\Laravel\Apie\Plugins\Illuminate\DataLayers\\'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|