1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\FixturesBundle\Parser; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Kunstmaan\FixturesBundle\Loader\Fixture; |
7
|
|
|
use Kunstmaan\FixturesBundle\Parser\Property\PropertyParserInterface; |
8
|
|
|
use Kunstmaan\FixturesBundle\Parser\Spec\SpecParserInterface; |
9
|
|
|
|
10
|
|
|
class Parser |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var PropertyParserInterface[] |
14
|
|
|
*/ |
15
|
|
|
private $parsers; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var SpecParserInterface[] |
19
|
|
|
*/ |
20
|
|
|
private $specParsers; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->parsers = new ArrayCollection(); |
|
|
|
|
25
|
|
|
$this->specParsers = new ArrayCollection(); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function parseFixture(Fixture $fixture, $providers, $fixtures = []) |
29
|
|
|
{ |
30
|
|
|
$entities = []; |
31
|
|
|
foreach ($fixtures as $key => $ref) { |
32
|
|
|
$entities[$key] = $ref->getEntity(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$fixture->setProperties($this->parseArray($fixture->getProperties(), $providers, $entities, [ |
36
|
|
|
$fixture, |
37
|
|
|
'fixtures' => $fixtures, |
38
|
|
|
])); |
39
|
|
|
|
40
|
|
|
$fixture->setParameters($this->parseArray($fixture->getParameters(), $providers, $fixtures, [ |
41
|
|
|
$fixture, |
42
|
|
|
'fixtures' => $fixtures, |
43
|
|
|
])); |
44
|
|
|
|
45
|
|
|
$fixture->setTranslations($this->parseArray($fixture->getTranslations(), $providers, $fixtures, [ |
46
|
|
|
$fixture, |
47
|
|
|
'fixtures' => $fixtures, |
48
|
|
|
])); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function parseEntity($entity, $providers, $fixtures = [], $additional = []) |
52
|
|
|
{ |
53
|
|
|
$refl = new \ReflectionClass($entity); |
54
|
|
|
$properties = $refl->getProperties(); |
55
|
|
|
|
56
|
|
|
foreach ($properties as $property) { |
57
|
|
|
$property->setAccessible(true); |
58
|
|
|
$value = $property->getValue($entity); |
59
|
|
|
|
60
|
|
|
foreach ($this->parsers as $parser) { |
61
|
|
|
if ($parser->canParse($value)) { |
62
|
|
|
$value = $parser->parse($value, $providers, $fixtures, $additional); |
63
|
|
|
$property->setValue($entity, $value); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function parseArray($array, $providers, $fixtures = [], $additional = []) |
70
|
|
|
{ |
71
|
|
|
if (empty($array)) { |
72
|
|
|
return $array; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach ($array as $key => $item) { |
76
|
|
|
if (is_array($item)) { |
77
|
|
|
$array[$key] = $this->parseArray($item, $providers, $fixtures, $additional); |
78
|
|
|
} else { |
79
|
|
|
$array[$key] = $this->parse($item, $providers, $fixtures, $additional); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $array; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function parse($value, $providers, $fixtures = [], $additional = []) |
87
|
|
|
{ |
88
|
|
|
foreach ($this->parsers as $parser) { |
89
|
|
|
if ($parser->canParse($value)) { |
90
|
|
|
$value = $parser->parse($value, $providers, $fixtures, $additional); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $value; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function parseSpec($value, $fixture, $fixtures = []) |
98
|
|
|
{ |
99
|
|
|
foreach ($this->specParsers as $parser) { |
100
|
|
|
if ($parser->canParse($value)) { |
101
|
|
|
return $parser->parse($fixture, $fixtures, $value); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$fixtures[$value] = $fixture; |
106
|
|
|
|
107
|
|
|
return $fixtures; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function addParser(PropertyParserInterface $parser, $alias) |
111
|
|
|
{ |
112
|
|
|
$this->parsers->set($alias, $parser); |
|
|
|
|
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function addSpecParser(SpecParserInterface $parser, $alias) |
118
|
|
|
{ |
119
|
|
|
$this->specParsers->set($alias, $parser); |
|
|
|
|
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..