1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Shapin\Datagen; |
6
|
|
|
|
7
|
|
|
class Loader |
8
|
|
|
{ |
9
|
|
|
private $fixtureSorter; |
10
|
|
|
|
11
|
|
|
private $fixtures = []; |
12
|
|
|
private $groups = []; |
13
|
|
|
|
14
|
7 |
|
public function __construct(FixtureSorter $fixtureSorter = null) |
15
|
|
|
{ |
16
|
7 |
|
$this->fixtureSorter = $fixtureSorter ?? new FixtureSorter(); |
17
|
7 |
|
} |
18
|
|
|
|
19
|
7 |
|
public function addFixture(FixtureInterface $fixture, array $groups = []): void |
20
|
|
|
{ |
21
|
7 |
|
$key = $this->getUniqueKey($fixture); |
22
|
|
|
|
23
|
7 |
|
$this->fixtures[$key] = $fixture; |
24
|
|
|
|
25
|
7 |
|
foreach ($groups as $group) { |
26
|
5 |
|
if (!isset($this->groups[$group])) { |
27
|
5 |
|
$this->groups[$group] = []; |
28
|
|
|
} |
29
|
5 |
|
if (!in_array($key, $this->groups[$group])) { |
30
|
5 |
|
$this->groups[$group][] = $key; |
31
|
|
|
} |
32
|
|
|
} |
33
|
7 |
|
} |
34
|
|
|
|
35
|
|
|
public function getFixturesByGroups(): array |
36
|
|
|
{ |
37
|
|
|
$groups = []; |
38
|
|
|
|
39
|
|
|
foreach ($this->groups as $name => $fixtures) { |
40
|
|
|
$groupFixtures = []; |
41
|
|
|
|
42
|
|
|
foreach ($fixtures as $fixtureUniqueKey) { |
43
|
|
|
$groupFixtures[] = $this->fixtures[$fixtureUniqueKey]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$groups[$name] = $this->fixtureSorter->sort($groupFixtures); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $groups; |
50
|
|
|
} |
51
|
|
|
|
52
|
7 |
|
public function getFixtures(array $groups = [], array $excludeGroups = []): array |
53
|
|
|
{ |
54
|
7 |
|
$duplicatedGroups = array_intersect($groups, $excludeGroups); |
55
|
7 |
|
if (0 < count($duplicatedGroups)) { |
56
|
1 |
|
throw new \InvalidArgumentException(sprintf('You can\'t both select & ignore a given group. Errored: ["%s"]', implode('", "', $duplicatedGroups))); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// Check that all groups exists. |
60
|
6 |
|
foreach ($groups + $excludeGroups as $group) { |
61
|
3 |
|
if (!isset($this->groups[$group])) { |
62
|
1 |
|
throw new \InvalidArgumentException(sprintf('Unknown group "%s". Available: ["%s"]', $group, implode('", "', array_keys($this->groups)))); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Select all relevant fixtures according to asked groups |
67
|
5 |
|
if (0 === count($groups)) { |
68
|
4 |
|
$fixtures = $this->fixtures; |
69
|
|
|
} else { |
70
|
1 |
|
$fixtures = []; |
71
|
1 |
|
foreach ($groups as $group) { |
72
|
1 |
|
foreach ($this->groups[$group] as $fixtureInGroup) { |
73
|
1 |
|
$fixtures[$fixtureInGroup] = $this->fixtures[$fixtureInGroup]; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Remove all fixtures to exclude |
79
|
5 |
|
foreach ($excludeGroups as $excludeGroup) { |
80
|
1 |
|
foreach ($this->groups[$excludeGroup] as $fixtureToExclude) { |
81
|
1 |
|
if (array_key_exists($fixtureToExclude, $fixtures)) { |
82
|
1 |
|
unset($fixtures[$fixtureToExclude]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
5 |
|
$fixtures = $this->fixtureSorter->sort($fixtures); |
88
|
|
|
|
89
|
5 |
|
return $fixtures; |
90
|
|
|
} |
91
|
|
|
|
92
|
7 |
|
private function getUniqueKey(FixtureInterface $fixture): string |
93
|
|
|
{ |
94
|
7 |
|
return $fixture->getProcessor().'-'.$fixture->getName(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|