1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiFaker; |
6
|
|
|
|
7
|
|
|
use VGirol\JsonApiFaker\Contract\GeneratorContract; |
8
|
|
|
use VGirol\JsonApiFaker\Factory\CollectionFactory; |
9
|
|
|
use VGirol\JsonApiFaker\Factory\DocumentFactory; |
10
|
|
|
use VGirol\JsonApiFaker\Factory\ErrorFactory; |
11
|
|
|
use VGirol\JsonApiFaker\Factory\JsonapiFactory; |
12
|
|
|
use VGirol\JsonApiFaker\Factory\RelationshipFactory; |
13
|
|
|
use VGirol\JsonApiFaker\Factory\ResourceIdentifierFactory; |
14
|
|
|
use VGirol\JsonApiFaker\Factory\ResourceObjectFactory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class is an helper to generate factories. |
18
|
|
|
*/ |
19
|
|
|
class Generator implements GeneratorContract |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* All the available factories |
23
|
|
|
* |
24
|
|
|
* @var array<string,string> |
25
|
|
|
*/ |
26
|
|
|
private $factories; |
27
|
|
|
|
28
|
|
|
/** |
29
|
8 |
|
* Class constructor |
30
|
|
|
*/ |
31
|
8 |
|
public function __construct() |
32
|
8 |
|
{ |
33
|
|
|
$this->setDefaultFactories(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public function setFactory(string $key, ?string $class) |
40
|
|
|
{ |
41
|
|
|
$this->factories[$key] = $class; |
42
|
1 |
|
|
43
|
|
|
return $this; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
1 |
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
|
|
public function create(string $alias, ...$args) |
50
|
|
|
{ |
51
|
|
|
$className = $this->getClassName($alias); |
52
|
|
|
$factory = new $className(...$args); |
53
|
|
|
$factory->setGenerator($this); |
54
|
|
|
|
55
|
|
|
return $factory; |
56
|
|
|
} |
57
|
|
|
|
58
|
8 |
|
/** |
59
|
|
|
* @inheritDoc |
60
|
8 |
|
*/ |
61
|
|
|
public function collection(...$args) |
62
|
7 |
|
{ |
63
|
|
|
return $this->create('collection', ...$args); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function document(...$args) |
70
|
|
|
{ |
71
|
|
|
return $this->create('document', ...$args); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function error(...$args) |
78
|
|
|
{ |
79
|
|
|
return $this->create('error', ...$args); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
1 |
|
*/ |
85
|
|
|
public function jsonapiObject(...$args) |
86
|
1 |
|
{ |
87
|
|
|
return $this->create('jsonapi', ...$args); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function relationship(...$args) |
94
|
|
|
{ |
95
|
|
|
return $this->create('relationship', ...$args); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
1 |
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
|
|
public function resourceIdentifier(...$args) |
102
|
|
|
{ |
103
|
|
|
return $this->create('resource-identifier', ...$args); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @inheritDoc |
108
|
1 |
|
*/ |
109
|
|
|
public function resourceObject(...$args) |
110
|
1 |
|
{ |
111
|
|
|
return $this->create('resource-object', ...$args); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the default factories. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
private function setDefaultFactories() |
120
|
1 |
|
{ |
121
|
|
|
$this->factories = [ |
122
|
1 |
|
'collection' => CollectionFactory::class, |
123
|
|
|
'document' => DocumentFactory::class, |
124
|
|
|
'error' => ErrorFactory::class, |
125
|
|
|
'jsonapi' => JsonapiFactory::class, |
126
|
|
|
'relationship' => RelationshipFactory::class, |
127
|
|
|
'resource-identifier' => ResourceIdentifierFactory::class, |
128
|
|
|
'resource-object' => ResourceObjectFactory::class |
129
|
|
|
]; |
130
|
8 |
|
} |
131
|
|
|
|
132
|
8 |
|
/** |
133
|
|
|
* Retrieve the factory class name |
134
|
|
|
* |
135
|
|
|
* @param string $key |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
* @throws \Exception |
139
|
8 |
|
*/ |
140
|
|
|
private function getClassName(string $key): string |
141
|
|
|
{ |
142
|
|
|
if (!array_key_exists($key, $this->factories)) { |
143
|
|
|
throw new \Exception( |
144
|
|
|
sprintf(Messages::FACTORY_INEXISTANT_KEY, $key) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($this->factories[$key] === null) { |
|
|
|
|
149
|
8 |
|
throw new \Exception( |
150
|
|
|
sprintf(Messages::FACTORY_FORBIDDEN_KEY, $key) |
151
|
8 |
|
); |
152
|
1 |
|
} |
153
|
1 |
|
|
154
|
|
|
return $this->factories[$key]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|