1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZLoremIpsumBundle\Core\ReferenceResolver; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\Core\MVC\ConfigResolverInterface; |
6
|
|
|
use Faker\Factory; |
7
|
|
|
use Kaliop\eZMigrationBundle\Core\ReferenceResolver\AbstractResolver; |
8
|
|
|
use Kaliop\eZMigrationBundle\API\EnumerableReferenceResolverInterface; |
9
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface; |
10
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
11
|
|
|
|
12
|
|
|
class FakerResolver extends AbstractResolver implements EnumerableReferenceResolverInterface |
13
|
|
|
{ |
14
|
|
|
protected $referencePrefixes = array('faker:'); |
15
|
|
|
|
16
|
|
|
/** @var \Faker\Generator $faker */ |
17
|
|
|
protected $faker; |
18
|
|
|
|
19
|
|
|
protected $referenceResolver; |
20
|
|
|
|
21
|
|
|
public function __construct(ConfigResolverInterface $configResolver, ReferenceResolverInterface $referenceResolver, |
22
|
|
|
$locale = null, $extraProviders = array()) |
23
|
|
|
{ |
24
|
|
|
parent::__construct(); |
25
|
|
|
|
26
|
|
|
if ($locale === null) { |
27
|
|
|
$locales = $configResolver->getParameter('languages'); |
28
|
|
|
$locale = reset($locales); |
29
|
|
|
} |
30
|
|
|
$locale = $this->convertLocale($locale); |
31
|
|
|
$this->referenceResolver = $referenceResolver; |
32
|
|
|
|
33
|
|
|
$this->faker = Factory::create($locale); |
34
|
|
|
foreach($extraProviders as $extraProvider) { |
35
|
|
|
$this->faker->addProvider(new $extraProvider($this->faker)); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $identifier format: 'faker:name', 'faker:randomnumber(3)' |
41
|
|
|
* @return mixed |
42
|
|
|
* @throws \Exception When trying to retrieve an unset reference |
43
|
|
|
*/ |
44
|
|
|
public function getReferenceValue($identifier) |
45
|
|
|
{ |
46
|
|
|
$identifier = trim($this->getReferenceIdentifier($identifier)); |
47
|
|
|
|
48
|
|
|
/* |
49
|
|
|
// original idea: regexp-based parsing |
50
|
|
|
// supported: |
51
|
|
|
// someOperator |
52
|
|
|
// someOperator(a, b) |
53
|
|
|
// someOperator|filter |
54
|
|
|
// someOperator(a, b)|filter |
55
|
|
|
// someOperator(a, b)|filter(c, d) |
56
|
|
|
/// @todo this way of matching args does not allow for arguments which contain a comma, right parenthesis or pipe chars... |
57
|
|
|
if (!preg_match('/^([^(|]+)(?:\(([^)]+)\))?(?:\|(unique|optional|valid)(?:\(([^)]+)\))?)?$/', $identifier, $matches)) { |
58
|
|
|
throw new \Exception("Could not parse as faker identifier ths string '$identifier'."); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$formatter = trim($matches[1]); |
62
|
|
|
$args = isset($matches[2]) ? trim($matches[2]) : ''; |
63
|
|
|
$filter = isset($matches[3]) ? trim($matches[3]) : ''; |
64
|
|
|
$filterArgs = isset($matches[4]) ? trim($matches[4]) : ''; |
65
|
|
|
if ($args != '') |
66
|
|
|
{ |
67
|
|
|
/// @todo convert true, false to bools, strip strings of quotes |
68
|
|
|
$arguments = array_map('trim', explode(',', $args)); |
69
|
|
|
} else { |
70
|
|
|
$arguments = array(); |
71
|
|
|
} |
72
|
|
|
if ($filterArgs != '') |
73
|
|
|
{ |
74
|
|
|
/// @todo convert true, false to bools, strip strings of quotes |
75
|
|
|
$filterArguments = array_map('trim', explode(',', $filterArgs)); |
76
|
|
|
} |
77
|
|
|
else { |
78
|
|
|
$filterArguments = array(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$faker = $this->faker; |
82
|
|
|
|
83
|
|
|
if ($filter != '') { |
84
|
|
|
$faker = $this->faker->format($filter, $filterArguments); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $faker->format($formatter, $arguments); |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
// alternative idea: use symfony/expression-language |
91
|
|
|
$expressionLanguage = new ExpressionLanguage(); |
92
|
|
|
|
93
|
|
|
$resolver = $this->referenceResolver; |
94
|
|
|
|
95
|
|
|
$expressionLanguage->register( |
96
|
|
|
'resolve', |
97
|
|
|
function ($str) { |
|
|
|
|
98
|
|
|
/// @todo we could implement this via eg a static class var which holds a pointer to $this->referenceResolver |
99
|
|
|
//return sprintf('(is_string(%1$s) ? FakerResolver::resolveExpressionLanguageReference(%1$s) : %1$s)', $str); |
100
|
|
|
return "throw new \Exception('The \'resolve\' expression language operator can not be compiled, only evaluated'"; |
101
|
|
|
}, |
102
|
|
|
function ($arguments, $str) use ($resolver) { |
103
|
|
|
if (!is_string($str)) { |
104
|
|
|
return $str; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $resolver->resolveReference($str); |
108
|
|
|
} |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return $expressionLanguage->evaluate('faker.' . $identifier, array( |
112
|
|
|
'faker' => $this->faker, |
113
|
|
|
)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* converts eng-GB in en_GB |
118
|
|
|
* @param $locale string |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
protected function convertLocale($locale) |
122
|
|
|
{ |
123
|
|
|
return substr($locale, 0, 2) . '_' . substr($locale, 4); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* We implement this method (interface) purely for convenience, as it allows this resolver to be added to the |
128
|
|
|
* 'custom-reference-resolver' chain and not break migration suspend/resume |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function listReferences() |
132
|
|
|
{ |
133
|
|
|
return array(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.