|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleMapper\Structure; |
|
4
|
|
|
|
|
5
|
|
|
use SimpleMapper\ActiveRow; |
|
6
|
|
|
use SimpleMapper\Exception\SimpleMapperException; |
|
7
|
|
|
use SimpleMapper\Repository; |
|
8
|
|
|
use SimpleMapper\Scope\Scope; |
|
9
|
|
|
use SimpleMapper\Selection; |
|
10
|
|
|
|
|
11
|
|
|
class CustomStructure implements Structure |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
protected $data = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $table |
|
18
|
|
|
* @param string|null $activeRowClass |
|
19
|
|
|
* @param string|null $selectionClass |
|
20
|
|
|
* @param Repository|null $repository |
|
21
|
|
|
* @return CustomStructure |
|
22
|
|
|
* @throws SimpleMapperException |
|
23
|
|
|
*/ |
|
24
|
|
|
public function registerTable(string $table, string $activeRowClass = null, string $selectionClass = null, Repository $repository = null): CustomStructure |
|
25
|
|
|
{ |
|
26
|
|
|
if ($activeRowClass) { |
|
|
|
|
|
|
27
|
|
|
$this->data[$table]['row'] = $activeRowClass; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if ($selectionClass) { |
|
|
|
|
|
|
31
|
|
|
$this->data[$table]['selection'] = $selectionClass; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if ($repository) { |
|
35
|
|
|
foreach ($repository->getScopes() as $scope) { |
|
36
|
|
|
if (!($scope instanceof Scope)) { |
|
37
|
|
|
throw new SimpleMapperException('Scopes can be only of class ' . Scope::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$this->data[$table]['scopes'][$scope->getName()] = $scope; |
|
41
|
|
|
} |
|
42
|
|
|
$repository->setStructure($this); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Fetch row class by table |
|
50
|
|
|
* @param string $table |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getActiveRowClass(string $table): string |
|
54
|
|
|
{ |
|
55
|
|
|
return isset($this->data[$table]['row']) |
|
56
|
|
|
? $this->data[$table]['row'] |
|
57
|
|
|
: ActiveRow::class; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Fetch selection class by table |
|
62
|
|
|
* @param string $table |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getSelectionClass(string $table): string |
|
66
|
|
|
{ |
|
67
|
|
|
return isset($this->data[$table]['selection']) |
|
68
|
|
|
? $this->data[$table]['selection'] |
|
69
|
|
|
: Selection::class; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns all scopes registered for table |
|
74
|
|
|
* @param string $table |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getScopes(string $table): array |
|
78
|
|
|
{ |
|
79
|
|
|
return isset($this->data[$table]['scopes']) |
|
80
|
|
|
? $this->data[$table]['scopes'] |
|
81
|
|
|
: []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns one scope |
|
86
|
|
|
* @param string $table |
|
87
|
|
|
* @param string $scope |
|
88
|
|
|
* @return Scope|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getScope(string $table, string $scope): ?Scope |
|
91
|
|
|
{ |
|
92
|
|
|
return isset($this->data[$table]['scopes'][$scope]) |
|
93
|
|
|
? $this->data[$table]['scopes'][$scope] |
|
94
|
|
|
: null; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: