|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiFaker; |
|
6
|
|
|
|
|
7
|
|
|
use VGirol\JsonApiFaker\Factory\CollectionFactory; |
|
8
|
|
|
use VGirol\JsonApiFaker\Factory\JsonapiFactory; |
|
9
|
|
|
use VGirol\JsonApiFaker\Factory\RelationshipFactory; |
|
10
|
|
|
use VGirol\JsonApiFaker\Factory\ResourceIdentifierFactory; |
|
11
|
|
|
use VGirol\JsonApiFaker\Factory\ResourceObjectFactory; |
|
12
|
|
|
|
|
13
|
|
|
class Generator |
|
14
|
|
|
{ |
|
15
|
|
|
private $factories; |
|
16
|
|
|
|
|
17
|
8 |
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
8 |
|
$this->setDefaultFactories(); |
|
20
|
8 |
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public function setFactory(string $key, string $class) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->factories[$key] = $class; |
|
25
|
|
|
|
|
26
|
1 |
|
return $this; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
8 |
|
public function create($alias, ...$args) |
|
30
|
|
|
{ |
|
31
|
8 |
|
$className = $this->getClassName($alias); |
|
32
|
|
|
|
|
33
|
7 |
|
return new $className(...$args); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function collection(...$args) |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $this->create('collection', ...$args); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function jsonapiObject(...$args) |
|
42
|
|
|
{ |
|
43
|
1 |
|
return $this->create('jsonapi', ...$args); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function relationship(...$args) |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->create('relationship', ...$args); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function resourceIdentifier(...$args) |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->create('resource-identifier', ...$args); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function resourceObject(...$args) |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->create('resource-object', ...$args); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
8 |
|
private function setDefaultFactories() |
|
62
|
|
|
{ |
|
63
|
8 |
|
$this->factories = [ |
|
64
|
|
|
'collection' => CollectionFactory::class, |
|
65
|
|
|
'jsonapi' => JsonapiFactory::class, |
|
66
|
|
|
'relationship' => RelationshipFactory::class, |
|
67
|
|
|
'resource-identifier' => ResourceIdentifierFactory::class, |
|
68
|
|
|
'resource-object' => ResourceObjectFactory::class |
|
69
|
|
|
]; |
|
70
|
8 |
|
} |
|
71
|
|
|
|
|
72
|
8 |
|
private function getClassName(string $key): string |
|
73
|
|
|
{ |
|
74
|
8 |
|
if (!isset($this->factories[$key])) { |
|
75
|
1 |
|
throw new \Exception( |
|
76
|
1 |
|
sprintf(Messages::FACTORY_INEXISTANT_KEY, $key) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
7 |
|
return $this->factories[$key]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|