1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Fakerino package. |
4
|
|
|
* |
5
|
|
|
* (c) Nicola Pietroluongo <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fakerino\Core; |
12
|
|
|
|
13
|
|
|
use Fakerino\Core\Database\DbInterface; |
14
|
|
|
use Fakerino\Core\FakeHandler\HandlerInterface; |
15
|
|
|
use Fakerino\Core\Filler\DbFiller; |
16
|
|
|
use Fakerino\Core\Filler\EntityFiller; |
17
|
|
|
use Fakerino\Core\Template\TemplateInterface; |
18
|
|
|
use Fakerino\FakeData\Exception\MissingRequiredOptionException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class FakeDataFactory, |
22
|
|
|
* generates fake data |
23
|
|
|
* |
24
|
|
|
* @author Nicola Pietroluongo <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class FakeDataFactory implements FakeDataFactoryInterface |
27
|
|
|
{ |
28
|
|
|
/** @var array */ |
29
|
|
|
private $out; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $outString; |
33
|
|
|
|
34
|
|
|
/** @var string|array */ |
35
|
|
|
private $startElement; |
36
|
|
|
|
37
|
|
|
/** @var \Fakerino\Core\FakeHandler\HandlerInterface */ |
38
|
|
|
private $fakeHandler; |
39
|
|
|
|
40
|
|
|
/** @var \Fakerino\Core\Database\DbInterface */ |
41
|
|
|
private $db; |
42
|
|
|
|
43
|
|
|
/** @var \Fakerino\Core\Template\TemplateInterface */ |
44
|
|
|
private $template; |
45
|
|
|
|
46
|
|
|
/** @var int */ |
47
|
|
|
private $num = 1; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* It receives: |
51
|
|
|
* the handlers priority for the fake request, |
52
|
|
|
* the DbInterface for the fakeTable operation, |
53
|
|
|
* and the TemplateInterface for the fakeTemplate operation. |
54
|
|
|
* |
55
|
|
|
* @param HandlerInterface $fakeHandler |
56
|
|
|
* @param DbInterface $db |
57
|
|
|
* @param TemplateInterface $template |
58
|
|
|
*/ |
59
|
60 |
|
public function __construct( |
60
|
|
|
HandlerInterface $fakeHandler, |
61
|
|
|
DbInterface $db, |
62
|
|
|
TemplateInterface $template |
63
|
|
|
) |
64
|
|
|
{ |
65
|
60 |
|
$this->fakeHandler = $fakeHandler; |
66
|
60 |
|
$this->db = $db; |
67
|
60 |
|
$this->template = $template; |
68
|
60 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Setups the fake element and initializes the output. |
72
|
|
|
* |
73
|
|
|
* @param string|array|null $elementName |
74
|
|
|
* |
75
|
|
|
* @return FakeDataFactory $this |
76
|
|
|
* @throws \Fakerino\FakeData\Exception\MissingRequiredOptionException |
77
|
|
|
*/ |
78
|
55 |
|
public function fake($elementName = null) |
79
|
|
|
{ |
80
|
55 |
|
if ($elementName === null) { |
81
|
1 |
|
throw new MissingRequiredOptionException('element to fake'); |
82
|
|
|
} |
83
|
54 |
|
$this->startElement = $elementName; |
84
|
54 |
|
$this->out = array(); |
85
|
54 |
|
$this->outString = null; |
86
|
|
|
|
87
|
54 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Fills the given entity with fake data. |
92
|
|
|
* |
93
|
|
|
* @param object|null $entity |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
* @throws \Fakerino\FakeData\Exception\MissingRequiredOptionException |
97
|
|
|
*/ |
98
|
2 |
|
public function fakeEntity($entity = null) |
99
|
|
|
{ |
100
|
2 |
|
if ($entity === null) { |
101
|
1 |
|
throw new MissingRequiredOptionException('entity to fake'); |
102
|
|
|
} |
103
|
1 |
|
$entityFiller = new EntityFiller($entity, $this); |
104
|
|
|
|
105
|
1 |
|
return $entityFiller->fill(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Fills the given table with fake data. |
110
|
|
|
* |
111
|
|
|
* @param string|null $tableName |
112
|
|
|
* |
113
|
|
|
* @return array $rows |
114
|
|
|
* @throws \Fakerino\FakeData\Exception\MissingRequiredOptionException |
115
|
|
|
*/ |
116
|
2 |
|
public function fakeTable($tableName = null) |
117
|
|
|
{ |
118
|
2 |
|
if (null === $tableName) { |
119
|
1 |
|
throw new MissingRequiredOptionException('table name'); |
120
|
|
|
} |
121
|
1 |
|
$dbFiller = new DbFiller($this->db, $tableName, $this, $this->num); |
122
|
1 |
|
$rows = $dbFiller->fill(); |
123
|
|
|
|
124
|
1 |
|
return $rows; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Fakes a template file. |
129
|
|
|
* |
130
|
|
|
* @param string $file |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
6 |
|
public function fakeTemplate($file) |
135
|
|
|
{ |
136
|
6 |
|
$this->template->loadTemplate($file); |
137
|
6 |
|
$varsName = $this->template->getVariables(); |
138
|
6 |
|
$out = ''; |
139
|
6 |
|
$num = $this->num; |
140
|
6 |
|
for ($i = 0; $i < $num; $i++) { |
141
|
6 |
|
$fakeData = $this->num(1)->fake($varsName)->toArray(); |
142
|
6 |
|
$data = array_combine(array_values($varsName), $fakeData); |
143
|
6 |
|
$out .= $this->template->render($data); |
144
|
6 |
|
} |
145
|
|
|
|
146
|
6 |
|
return $out; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Sets $num for iterate the fake process. |
151
|
|
|
* |
152
|
|
|
* @param int $num |
153
|
|
|
* |
154
|
|
|
* @return FakeDataFactory $this |
155
|
|
|
* @throws \Exception |
156
|
|
|
*/ |
157
|
30 |
|
public function num($num = 1) |
158
|
|
|
{ |
159
|
30 |
|
$this->num = $num; |
160
|
|
|
|
161
|
30 |
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param int|null $seed |
166
|
|
|
* |
167
|
|
|
* @return FakeDataFactory $this |
168
|
|
|
*/ |
169
|
|
|
public function seed($seed = null) |
170
|
|
|
{ |
171
|
|
|
if ($seed !== null) { |
172
|
|
|
mt_srand($seed); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
13 |
|
public function toArray() |
182
|
|
|
{ |
183
|
13 |
|
$this->startFake($this->startElement, $this->num); |
184
|
|
|
|
185
|
13 |
|
return $this->out; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return string json |
190
|
|
|
*/ |
191
|
4 |
|
public function toJson() |
192
|
|
|
{ |
193
|
4 |
|
$this->startFake($this->startElement, $this->num); |
194
|
|
|
|
195
|
4 |
|
return json_encode($this->out); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return string |
200
|
|
|
*/ |
201
|
35 |
|
public function __toString() |
202
|
|
|
{ |
203
|
35 |
|
$this->startFake($this->startElement, $this->num); |
204
|
35 |
|
array_walk_recursive($this->out, array($this, 'arrayToString')); |
205
|
35 |
|
$this->outString = substr($this->outString, 0, -1); |
206
|
|
|
|
207
|
35 |
|
return $this->outString; |
208
|
|
|
} |
209
|
|
|
|
210
|
51 |
|
private function startFake($elementName, $num) |
211
|
|
|
{ |
212
|
51 |
|
$out = array(); |
213
|
51 |
|
$elementToFake = $this->getElementToFake($elementName); |
214
|
51 |
|
for ($i = 0; $i < $num; $i++) { |
215
|
51 |
|
foreach ($elementToFake as $key => $val) { |
216
|
51 |
|
$element = new FakeElement($key, $val); |
217
|
51 |
|
$out[] = $this->fakeHandler->handle($element); |
218
|
51 |
|
} |
219
|
51 |
|
} |
220
|
51 |
|
$this->out = $out; |
221
|
51 |
|
} |
222
|
|
|
|
223
|
51 |
|
private function getElementToFake($elementsName) |
224
|
|
|
{ |
225
|
51 |
|
if (!is_array($elementsName)) { |
226
|
|
|
|
227
|
25 |
|
return array($elementsName); |
228
|
|
|
} |
229
|
|
|
|
230
|
27 |
|
return $elementsName; |
231
|
|
|
} |
232
|
|
|
|
233
|
35 |
|
private function arrayToString($arr) |
234
|
|
|
{ |
235
|
35 |
|
$this->outString .= $arr . PHP_EOL; |
236
|
|
|
} |
237
|
|
|
} |