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