1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiFaker\Factory; |
6
|
|
|
|
7
|
|
|
use VGirol\JsonApiConstant\Members; |
8
|
|
|
use VGirol\JsonApiFaker\Contract\ErrorContract; |
9
|
|
|
use VGirol\JsonApiFaker\Exception\JsonApiFakerException; |
10
|
|
|
use VGirol\JsonApiFaker\Messages; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A factory for error object |
14
|
|
|
*/ |
15
|
|
|
class ErrorFactory extends BaseFactory implements ErrorContract |
16
|
|
|
{ |
17
|
1 |
|
use HasIdentifier; |
18
|
1 |
|
use HasLinks; |
19
|
1 |
|
use HasMeta; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The "status" member |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $status; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The "code" member |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $code; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The "title" member |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $title; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The "details" member |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
public $details; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The "source" member |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
public $source; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set one of the following member : status, code, title, details |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* @param string $value |
61
|
|
|
* |
62
|
|
|
* @return static |
63
|
|
|
* @throws JsonApiFakerException |
64
|
|
|
*/ |
65
|
36 |
|
public function set(string $key, string $value) |
66
|
|
|
{ |
67
|
36 |
|
if (!property_exists($this, $key)) { |
68
|
3 |
|
throw new JsonApiFakerException(sprintf(Messages::ERROR_OBJECT_INEXISTANT_KEY, $key)); |
69
|
|
|
} |
70
|
|
|
|
71
|
33 |
|
$this->{$key} = $value; |
72
|
|
|
|
73
|
33 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the "source" member |
78
|
|
|
* |
79
|
|
|
* @param array $source |
80
|
|
|
* |
81
|
|
|
* @return static |
82
|
|
|
*/ |
83
|
18 |
|
public function setSource(array $source) |
84
|
|
|
{ |
85
|
18 |
|
$this->source = $source; |
86
|
|
|
|
87
|
18 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
15 |
|
public function toArray(): array |
94
|
|
|
{ |
95
|
15 |
|
$resource = []; |
96
|
15 |
|
if (isset($this->id)) { |
97
|
15 |
|
$resource[Members::ID] = $this->id; |
98
|
|
|
} |
99
|
15 |
|
if (isset($this->links)) { |
100
|
9 |
|
$resource[Members::LINKS] = $this->links; |
101
|
|
|
} |
102
|
15 |
|
if (isset($this->status)) { |
103
|
12 |
|
$resource[Members::ERROR_STATUS] = $this->status; |
104
|
|
|
} |
105
|
15 |
|
if (isset($this->code)) { |
106
|
12 |
|
$resource[Members::ERROR_CODE] = $this->code; |
107
|
|
|
} |
108
|
15 |
|
if (isset($this->title)) { |
109
|
9 |
|
$resource[Members::ERROR_TITLE] = $this->title; |
110
|
|
|
} |
111
|
15 |
|
if (isset($this->details)) { |
112
|
9 |
|
$resource[Members::ERROR_DETAILS] = $this->details; |
113
|
|
|
} |
114
|
15 |
|
if (isset($this->source)) { |
115
|
9 |
|
$resource[Members::ERROR_SOURCE] = $this->source; |
116
|
|
|
} |
117
|
15 |
|
if (isset($this->meta)) { |
118
|
12 |
|
$resource[Members::META] = $this->meta; |
119
|
|
|
} |
120
|
|
|
|
121
|
15 |
|
return $resource; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Fill the error object with fake values |
126
|
|
|
* ("id", "links", "meta", "status", "code", "title", "details" and "source"). |
127
|
|
|
* |
128
|
|
|
* @return static |
129
|
|
|
* @throws JsonApiFakerException |
130
|
|
|
*/ |
131
|
12 |
|
public function fake() |
132
|
|
|
{ |
133
|
12 |
|
return $this->fakeIdentifier() |
134
|
12 |
|
->fakeLinks([Members::LINK_ABOUT => ['url']]) |
135
|
12 |
|
->fakeMeta() |
136
|
12 |
|
->fakeStatus() |
137
|
12 |
|
->fakeCode() |
138
|
12 |
|
->fakeTitle() |
139
|
12 |
|
->fakeDetails() |
140
|
12 |
|
->fakeSource(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Fill the "status" member with fake value |
145
|
|
|
* |
146
|
|
|
* @return static |
147
|
|
|
* @throws JsonApiFakerException |
148
|
|
|
*/ |
149
|
12 |
|
private function fakeStatus() |
150
|
|
|
{ |
151
|
|
|
$allowedStatus = [ |
152
|
12 |
|
'200', '201', '202', '203', '204', '205', '206', '207', '208', '226', |
153
|
|
|
'300', '301', '302', '303', '304', '305', '307', '308', |
154
|
|
|
'400', '401', '402', '403', '404', '405', '406', '407', '408', '409', |
155
|
|
|
'410', '411', '412', '413', '414', '415', '416', '417', '422', '423', '424', '426', '428', '429', '431', |
156
|
|
|
'500', '501', '502', '503', '504', '505', '506', '507', '508', '510', '511' |
157
|
|
|
]; |
158
|
12 |
|
$faker = \Faker\Factory::create(); |
159
|
|
|
|
160
|
12 |
|
return $this->set(Members::ERROR_STATUS, strval($faker->randomElement($allowedStatus))); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Fill the "code" member with fake value |
165
|
|
|
* |
166
|
|
|
* @return static |
167
|
|
|
* @throws JsonApiFakerException |
168
|
|
|
*/ |
169
|
12 |
|
private function fakeCode() |
170
|
|
|
{ |
171
|
12 |
|
$faker = \Faker\Factory::create(); |
172
|
|
|
|
173
|
12 |
|
return $this->set(Members::ERROR_CODE, strval($faker->numberBetween(1, 100))); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Fill the "title" member with fake value |
178
|
|
|
* |
179
|
|
|
* @return static |
180
|
|
|
* @throws JsonApiFakerException |
181
|
|
|
*/ |
182
|
12 |
|
private function fakeTitle() |
183
|
|
|
{ |
184
|
12 |
|
$faker = \Faker\Factory::create(); |
185
|
|
|
|
186
|
12 |
|
return $this->set(Members::ERROR_TITLE, $faker->sentence); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Fill the "details" member with fake value |
191
|
|
|
* |
192
|
|
|
* @return static |
193
|
|
|
* @throws JsonApiFakerException |
194
|
|
|
*/ |
195
|
12 |
|
private function fakeDetails() |
196
|
|
|
{ |
197
|
12 |
|
$faker = \Faker\Factory::create(); |
198
|
|
|
|
199
|
12 |
|
return $this->set(Members::ERROR_DETAILS, $faker->paragraph); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Fill the "source" member with fake value |
204
|
|
|
* |
205
|
|
|
* @return static |
206
|
|
|
*/ |
207
|
12 |
|
private function fakeSource() |
208
|
|
|
{ |
209
|
12 |
|
$faker = \Faker\Factory::create(); |
210
|
|
|
|
211
|
12 |
|
return $this->setSource( |
212
|
|
|
[ |
213
|
12 |
|
Members::ERROR_PARAMETER => $faker->word |
214
|
|
|
] |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|