1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleMapper; |
6
|
|
|
|
7
|
|
|
use SimpleMapper\Structure\Structure; |
8
|
|
|
|
9
|
|
|
class Mapper |
10
|
|
|
{ |
11
|
|
|
/** @var Structure */ |
12
|
|
|
private $structure; |
13
|
|
|
|
14
|
|
|
/** @var array */ |
15
|
|
|
private $repositories = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param Structure $structure |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Structure $structure) |
21
|
|
|
{ |
22
|
|
|
$this->structure = $structure; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Map classes and scopes by repository |
27
|
|
|
* @param Repository $repository |
28
|
|
|
* @param string|null $activeRowClass |
29
|
|
|
* @param string|null $selectionClass |
30
|
|
|
* @return Mapper |
31
|
|
|
*/ |
32
|
|
|
public function mapRepository(Repository $repository, string $activeRowClass = null, string $selectionClass = null): Mapper |
33
|
|
|
{ |
34
|
|
|
$this->repositories[get_class($repository)] = $repository; |
35
|
|
|
$repository->setStructure($this->structure); |
36
|
|
|
|
37
|
|
|
if ($activeRowClass) { |
|
|
|
|
38
|
|
|
$this->structure->registerActiveRowClass($repository::getTableName(), $activeRowClass); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ($selectionClass) { |
|
|
|
|
42
|
|
|
$this->structure->registerSelectionClass($repository::getTableName(), $selectionClass); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Map classes only by table name |
50
|
|
|
* @param string $tableName |
51
|
|
|
* @param string|null $activeRowClass |
52
|
|
|
* @param string|null $selectionClass |
53
|
|
|
* @return Mapper |
54
|
|
|
*/ |
55
|
|
|
public function mapTableName(string $tableName, string $activeRowClass = null, string $selectionClass = null): Mapper |
56
|
|
|
{ |
57
|
|
|
if ($activeRowClass) { |
|
|
|
|
58
|
|
|
$this->structure->registerActiveRowClass($tableName, $activeRowClass); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($selectionClass) { |
|
|
|
|
62
|
|
|
$this->structure->registerSelectionClass($tableName, $selectionClass); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $class |
70
|
|
|
* @return null|Repository |
71
|
|
|
*/ |
72
|
|
|
public function getRepository(string $class): ?Repository |
73
|
|
|
{ |
74
|
|
|
return $this->repositories[$class] ?? null; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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: