1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiFaker\Factory; |
6
|
|
|
|
7
|
|
|
use Faker\Generator; |
8
|
|
|
use VGirol\JsonApiFaker\Contract\FactoryContract; |
9
|
|
|
use VGirol\JsonApiFaker\Contract\GeneratorContract; |
10
|
|
|
use VGirol\JsonApiFaker\Messages; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class implements some methods of the FactoryContract interface. |
14
|
|
|
*/ |
15
|
|
|
abstract class BaseFactory implements FactoryContract |
16
|
|
|
{ |
17
|
|
|
const FAKE_RESOURCE_OBJECT = 1; |
18
|
|
|
const FAKE_RESOURCE_IDENTIFIER = 2; |
19
|
|
|
const FAKE_SINGLE = 4; |
20
|
|
|
const FAKE_COLLECTION = 8; |
21
|
|
|
const FAKE_CAN_BE_NULL = 16; |
22
|
|
|
const FAKE_ERRORS = 32; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The factory generator |
26
|
19 |
|
* |
27
|
|
|
* @var GeneratorContract |
28
|
19 |
|
*/ |
29
|
19 |
|
public $generator; |
30
|
|
|
|
31
|
|
|
abstract public function toArray(): ?array; |
32
|
19 |
|
|
33
|
19 |
|
abstract public function fake(); |
34
|
|
|
|
35
|
2 |
|
/** |
36
|
|
|
* Set the Generator instance |
37
|
2 |
|
* |
38
|
1 |
|
* @param GeneratorContract $generator |
39
|
|
|
* |
40
|
|
|
* @return static |
41
|
2 |
|
*/ |
42
|
2 |
|
public function setGenerator($generator) |
43
|
|
|
{ |
44
|
|
|
$this->generator = $generator; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
2 |
|
|
49
|
|
|
public function addToObject(string $object, string $name, $value): void |
50
|
2 |
|
{ |
51
|
|
|
if (!isset($this->{$object})) { |
52
|
2 |
|
$this->{$object} = []; |
53
|
1 |
|
} |
54
|
1 |
|
|
55
|
|
|
$this->{$object}[$name] = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function addToArray(string $object, $value): void |
59
|
|
|
{ |
60
|
|
|
if (!isset($this->{$object})) { |
61
|
|
|
$this->{$object} = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->{$object}[] = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
* @throws \Exception |
70
|
|
|
*/ |
71
|
|
|
public function toJson($options = 0): string |
72
|
|
|
{ |
73
|
|
|
$json = json_encode($this->toArray(), $options); |
74
|
|
|
|
75
|
|
|
if ($json === false) { |
76
|
|
|
throw new \Exception( |
77
|
|
|
sprintf(Messages::JSON_ENCODE_ERROR, json_last_error_msg()) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $json; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This methods creates some members with fake names and values. |
86
|
|
|
* |
87
|
|
|
* The fake member is filled with the null value : |
88
|
|
|
* $options = [ |
89
|
|
|
* 'member name' => null |
90
|
16 |
|
* ] |
91
|
|
|
* |
92
|
16 |
|
* The default array of methods is used to fill the fake value : |
93
|
|
|
* $options = [ |
94
|
16 |
|
* 'member name' => [] |
95
|
15 |
|
* ] |
96
|
15 |
|
* |
97
|
15 |
|
* Only the provided methods are used to fill the fake value : |
98
|
15 |
|
* $options = [ |
99
|
|
|
* 'member name' => [ array of methods allowed to fake ] |
100
|
|
|
* ] |
101
|
|
|
* |
102
|
16 |
|
* If no key name is provided, a fake member name is generated : |
103
|
16 |
|
* $options = [ |
104
|
16 |
|
* null, |
105
|
16 |
|
* [], |
106
|
|
|
* [ array of methods allowed to fake ] |
107
|
16 |
|
* ] |
108
|
|
|
* |
109
|
|
|
* @param integer|array $options The number of keys to generate or an array of keys to use. |
110
|
16 |
|
* |
111
|
|
|
* @return array<string,mixed> |
112
|
|
|
*/ |
113
|
|
|
protected function fakeMembers($options): array |
114
|
|
|
{ |
115
|
|
|
$faker = \Faker\Factory::create(); |
116
|
|
|
|
117
|
|
|
if (is_int($options)) { |
118
|
|
|
$count = $options; |
119
|
|
|
$options = []; |
120
|
|
|
for ($i = 0; $i < $count; $i++) { |
121
|
17 |
|
$options[] = []; |
122
|
|
|
} |
123
|
17 |
|
} |
124
|
16 |
|
|
125
|
|
|
$values = []; |
126
|
16 |
|
foreach ($options as $key => $providers) { |
127
|
16 |
|
$name = $this->fakeMemberName($faker, $key); |
128
|
|
|
$value = $this->fakeValue($faker, $providers); |
129
|
|
|
|
130
|
17 |
|
$values[$name] = $value; |
131
|
17 |
|
} |
132
|
|
|
|
133
|
|
|
return $values; |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Returns a fake member's name. |
138
|
|
|
* |
139
|
|
|
* @param Generator $faker |
140
|
|
|
* @param string|integer|null $name |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
17 |
|
protected function fakeMemberName(Generator $faker, $name = null): string |
145
|
|
|
{ |
146
|
|
|
if (\is_null($name) || \is_int($name)) { |
147
|
17 |
|
$forbidden = ['id', 'type']; |
148
|
|
|
do { |
149
|
|
|
$name = $faker->unique()->word; |
150
|
|
|
} while (in_array($name, $forbidden)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (\strpos($name, '/') === false) { |
154
|
|
|
return $name; |
155
|
17 |
|
} |
156
|
1 |
|
|
157
|
|
|
return $faker->unique()->regexify($name); |
158
|
|
|
} |
159
|
17 |
|
|
160
|
17 |
|
/** |
161
|
|
|
* Returns a fake member's value. |
162
|
|
|
* |
163
|
17 |
|
* @param Generator $faker |
164
|
|
|
* @param array<string>|null $providers |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
|
|
protected function fakeValue(Generator $faker, $providers = []) |
168
|
|
|
{ |
169
|
|
|
$allowed = [ |
170
|
|
|
'word', |
171
|
|
|
'sentence', |
172
|
|
|
'boolean', |
173
|
|
|
'randomNumber', |
174
|
|
|
'randomFloat', |
175
|
|
|
'date' |
176
|
|
|
]; |
177
|
|
|
|
178
|
|
|
if (is_null($providers)) { |
179
|
|
|
return null; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$method = $faker->randomElement( |
183
|
|
|
empty($providers) ? $allowed : $providers |
184
|
|
|
); |
185
|
|
|
|
186
|
|
|
return $faker->{$method}; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|