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