|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: wechsler |
|
5
|
|
|
* Date: 03/03/2017 |
|
6
|
|
|
* Time: 20:09 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Phase\TakeATicket\SongLoader; |
|
10
|
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Connection; |
|
12
|
|
|
use Phase\TakeATicket\DataSource\Factory; |
|
13
|
|
|
|
|
14
|
|
|
class RowMapperManager |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string[] |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $rowMapperClasses = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Connection |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $dbConn; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var RowMapperInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $rowMappers; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* RowMapperManager constructor. |
|
34
|
|
|
* @param Connection $dbConn |
|
35
|
|
|
* @param \string[] $rowMapperClasses |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Connection $dbConn, array $rowMapperClasses) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->dbConn = $dbConn; |
|
40
|
|
|
$dataStore = Factory::datasourceFromDbConnection($dbConn); |
|
41
|
|
|
$this->rowMapperClasses = $rowMapperClasses; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
foreach ($rowMapperClasses as $class) { |
|
44
|
|
|
$this->validateClass($class); |
|
45
|
|
|
$mapper = new $class($dataStore); |
|
46
|
|
|
/** @var RowMapperInterface $mapper */ |
|
47
|
|
|
$this->rowMappers[strtolower($mapper->getShortName())] = $mapper; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the registered row mapper with the specified short name |
|
53
|
|
|
* |
|
54
|
|
|
* @param $shortName |
|
55
|
|
|
* @return null|RowMapperInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getRowMapperByShortName($shortName) |
|
58
|
|
|
{ |
|
59
|
|
|
$shortName = strtolower($shortName); |
|
60
|
|
|
return array_key_exists($shortName, $this->rowMappers) ? $this->rowMappers[$shortName] : null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the registered row mapper with the specified short name |
|
65
|
|
|
* |
|
66
|
|
|
* @param $shortName |
|
67
|
|
|
* @return null|RowMapperInterface |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getRowMapperClassByShortName($shortName) |
|
70
|
|
|
{ |
|
71
|
|
|
$mapper = $this->getRowMapperByShortName($shortName); |
|
72
|
|
|
return $mapper ? get_class($mapper) : null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getRowMapperShortNames() |
|
76
|
|
|
{ |
|
77
|
|
|
return array_keys($this->rowMappers); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return RowMapperInterface[] |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getRowMappers() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->rowMappers; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Ensure that provided classname is a valid RowMapperInterface |
|
90
|
|
|
* |
|
91
|
|
|
* @param $rowMapperClass |
|
92
|
|
|
* @throws \InvalidArgumentException |
|
93
|
|
|
*/ |
|
94
|
|
View Code Duplication |
protected function validateClass($rowMapperClass) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$class = new \ReflectionClass($rowMapperClass); |
|
97
|
|
|
if ($class->isSubclassOf(RowMapperInterface::class)) { |
|
98
|
|
|
$this->rowMapperClass = $rowMapperClass; |
|
|
|
|
|
|
99
|
|
|
} else { |
|
100
|
|
|
throw new \InvalidArgumentException( |
|
101
|
|
|
"$rowMapperClass must implement " . RowMapperInterface::class |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
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..