-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaker.php
More file actions
233 lines (199 loc) · 6.71 KB
/
Faker.php
File metadata and controls
233 lines (199 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\Alice\Instances\Processor\Methods;
use Nelmio\Alice\Instances\Collection;
use Nelmio\Alice\Instances\Processor\ProcessableInterface;
class Faker implements MethodInterface
{
/**
* @var Collection
*/
protected $objects;
/**
* Custom faker providers to use with faker generator
*
* @var array
*/
private $providers;
/**
* @var \Faker\Generator[]
*/
private $generators = [];
/**
* Default locale to use with faker
*
* @var string
*/
private $defaultLocale;
/**
* @var string
*/
private $valueForCurrent;
public function __construct(array $providers, $locale = 'en_US')
{
$this->providers = $providers;
$this->defaultLocale = $locale;
}
/**
* sets the object collection to handle referential calls
*
* @param Collection $objects
*/
public function setObjects(Collection $objects)
{
$this->objects = $objects;
}
/**
* sets the value for <current()>
*
* @param string
*/
public function setValueForCurrent($valueForCurrent)
{
$this->valueForCurrent = $valueForCurrent;
}
/**
* sets the providers that can be used
*
* @param array
*/
public function setProviders(array $providers)
{
$this->providers = $providers;
$this->generators = [];
}
/**
* Adds one or more providers that can be used
*
* @param object|array $provider Provider or array of providers
*/
public function addProvider($provider)
{
if (!is_array($provider)) {
$provider = [$provider];
}
foreach ($provider as $p) {
$this->providers[] = $p;
foreach ($this->generators as $generator) {
$generator->addProvider($p);
}
}
}
/**
* {@inheritDoc}
*/
public function canProcess(ProcessableInterface $processable)
{
return is_string($processable->getValue());
}
/**
* {@inheritDoc}
*/
public function process(ProcessableInterface $processable, array $variables)
{
$fakerRegex = '<(?:(?<locale>[a-z]+(?:_[a-z]+)?):)?(?<name>[a-z0-9_]+?)?\((?<args>(?:[^)]*|\)(?!>))*)\)>';
if ($processable->valueMatches('#^'.$fakerRegex.'$#i')) {
return $this->replacePlaceholder($processable->matches, $variables);
} else {
// format placeholders inline
return preg_replace_callback('#'.$fakerRegex.'#i', function ($matches) use ($variables) {
return $this->replacePlaceholder($matches, $variables);
}, $processable->getValue());
}
}
/**
* replaces a placeholder by the result of a ->fake call
*
* @param array $matches
* @param array $variables
* @return mixed
*/
public function replacePlaceholder($matches, array $variables)
{
$args = isset($matches['args']) && '' !== $matches['args'] ? $matches['args'] : null;
if (trim($matches['name']) == '') {
$matches['name'] = 'identity';
}
if (!$args) {
return $this->fake($matches['name'], $matches['locale']);
}
// replace references to other variables in the same object
$args = preg_replace_callback('{\{?\$([a-z0-9_]+)\}?}i', function ($match) use ($variables) {
if (array_key_exists($match[1], $variables)) {
return '$variables['.var_export($match[1], true).']';
}
return $match[0];
}, $args);
// replace references to other objects
$args = preg_replace_callback('{(?<string>".*?[^\\\\]")|(?:(?<multi>\d+)x )?(?<!\\\\)@(?<reference>[a-z0-9_.*]+)(?:\->(?<property>[a-z0-9_-]+))?}i', function ($match) {
if (!empty($match['string'])) {
return $match['string'];
}
$multi = ('' !== $match['multi']) ? $match['multi'] : null;
$property = isset($match['property']) ? $match['property'] : null;
if (strpos($match['reference'], '*')) {
return '$this->objects->random(' . var_export($match['reference'], true) . ', ' . var_export($multi, true) . ', ' . var_export($property, true) . ')';
}
if (null !== $multi) {
throw new \UnexpectedValueException('To use multiple references you must use a mask like "'.$match['multi'].'x @user*", otherwise you would always get only one item.');
}
return '$this->objects->find(' . var_export($match['reference'], true) . ', ' . var_export($property, true) . ')';
}, $args);
$locale = var_export($matches['locale'], true);
$name = var_export($matches['name'], true);
// enable calls to $fake() to call faker from within faker calls
$that = $this;
$fake = function () use ($that) {
return call_user_func_array([$that, 'fake'], func_get_args());
};
return eval('return $this->fake(' . $name . ', ' . $locale . ', ' . $args . ');');
}
/**
* Returns a fake value
*
* This is made public so it is accessible by the $fake() callback in replacePlaceholder
* and the callback in Parser\Method\Base::createFakerClosure
*
* @param string $formatter
* @param string $locale
* @private
* @return mixed
*/
public function fake($formatter, $locale = null)
{
$args = array_slice(func_get_args(), 2);
if ($formatter == 'current') {
if ($this->valueForCurrent === null) {
throw new \UnexpectedValueException('Cannot use <current()> out of fixtures ranges or enum');
}
return $this->valueForCurrent;
}
return $this->getGenerator($locale)->format($formatter, $args);
}
/**
* Get the generator for this locale
*
* @param string $locale the requested locale, defaults to constructor injected default
*
* @return \Faker\Generator the generator for the requested locale
*/
private function getGenerator($locale = null)
{
$locale = $locale ?: $this->defaultLocale;
if (!isset($this->generators[$locale])) {
$generator = \Faker\Factory::create($locale);
foreach ($this->providers as $provider) {
$generator->addProvider($provider);
}
$this->generators[$locale] = $generator;
}
return $this->generators[$locale];
}
}