1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleMapper; |
4
|
|
|
|
5
|
|
|
use SimpleMapper\Structure\Structure; |
6
|
|
|
|
7
|
|
|
class Mapper |
8
|
|
|
{ |
9
|
|
|
/** @var Structure */ |
10
|
|
|
private $structure; |
11
|
|
|
|
12
|
|
|
/** @var array */ |
13
|
|
|
private $repositories = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param Structure $structure |
17
|
|
|
*/ |
18
|
68 |
|
public function __construct(Structure $structure) |
19
|
|
|
{ |
20
|
68 |
|
$this->structure = $structure; |
21
|
68 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Map classes and scopes by repository |
25
|
|
|
* @param Repository $repository |
26
|
|
|
* @param string|null $activeRowClass |
27
|
|
|
* @param string|null $selectionClass |
28
|
|
|
* @return Mapper |
29
|
|
|
*/ |
30
|
68 |
|
public function mapRepository(Repository $repository, string $activeRowClass = null, string $selectionClass = null): Mapper |
31
|
|
|
{ |
32
|
68 |
|
$this->repositories[get_class($repository)] = $repository; |
33
|
68 |
|
$repository->setStructure($this->structure); |
34
|
|
|
|
35
|
68 |
|
if ($activeRowClass) { |
|
|
|
|
36
|
68 |
|
$this->structure->registerActiveRowClass($repository::getTableName(), $activeRowClass); |
37
|
|
|
} |
38
|
|
|
|
39
|
68 |
|
if ($selectionClass) { |
|
|
|
|
40
|
68 |
|
$this->structure->registerSelectionClass($repository::getTableName(), $selectionClass); |
41
|
|
|
} |
42
|
|
|
|
43
|
68 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Map classes only by table name |
48
|
|
|
* @param string $tableName |
49
|
|
|
* @param string|null $activeRowClass |
50
|
|
|
* @param string|null $selectionClass |
51
|
|
|
* @return Mapper |
52
|
|
|
*/ |
53
|
68 |
|
public function mapTableName(string $tableName, string $activeRowClass = null, string $selectionClass = null): Mapper |
54
|
|
|
{ |
55
|
68 |
|
if ($activeRowClass) { |
|
|
|
|
56
|
68 |
|
$this->structure->registerActiveRowClass($tableName, $activeRowClass); |
57
|
|
|
} |
58
|
|
|
|
59
|
68 |
|
if ($selectionClass) { |
|
|
|
|
60
|
68 |
|
$this->structure->registerSelectionClass($tableName, $selectionClass); |
61
|
|
|
} |
62
|
|
|
|
63
|
68 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $class |
68
|
|
|
* @return null|Repository |
69
|
|
|
*/ |
70
|
68 |
|
public function getRepository(string $class): ?Repository |
71
|
|
|
{ |
72
|
68 |
|
return $this->repositories[$class] ?? null; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: