1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SimpleMapper\Structure; |
5
|
|
|
|
6
|
|
|
use SimpleMapper\ActiveRow; |
7
|
|
|
use SimpleMapper\Exception\SimpleMapperException; |
8
|
|
|
use SimpleMapper\Scope\Scope; |
9
|
|
|
use SimpleMapper\Selection; |
10
|
|
|
|
11
|
|
|
class BaseStructure implements Structure |
12
|
|
|
{ |
13
|
|
|
/** @var array */ |
14
|
|
|
protected $data = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Register new active row class for table |
18
|
|
|
* @param string $tableName |
19
|
|
|
* @param string $activeRowClass |
20
|
|
|
* @return Structure|BaseStructure |
21
|
|
|
*/ |
22
|
72 |
|
public function registerActiveRowClass(string $tableName, string $activeRowClass): Structure |
23
|
|
|
{ |
24
|
72 |
|
$this->data[$tableName]['row'] = $activeRowClass; |
25
|
72 |
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register new selection class for table |
30
|
|
|
* @param string $tableName |
31
|
|
|
* @param string $selectionClass |
32
|
|
|
* @return Structure|BaseStructure |
33
|
|
|
*/ |
34
|
70 |
|
public function registerSelectionClass(string $tableName, string $selectionClass): Structure |
35
|
|
|
{ |
36
|
70 |
|
$this->data[$tableName]['selection'] = $selectionClass; |
37
|
70 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Register new scopes for table |
42
|
|
|
* @param string $tableName |
43
|
|
|
* @param array $scopes |
44
|
|
|
* @return Structure|BaseStructure |
45
|
|
|
* @throws SimpleMapperException |
46
|
|
|
*/ |
47
|
70 |
|
public function registerScopes(string $tableName, array $scopes): Structure |
48
|
|
|
{ |
49
|
70 |
|
foreach ($scopes as $scope) { |
50
|
70 |
|
if (!($scope instanceof Scope)) { |
51
|
|
|
throw new SimpleMapperException('Scopes can be only of class ' . Scope::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
70 |
|
$this->data[$tableName]['scopes'][$scope->getName()] = $scope; |
55
|
|
|
} |
56
|
70 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Fetch row class by table |
61
|
|
|
* @param string $tableName |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
54 |
|
public function getActiveRowClass(string $tableName): string |
65
|
|
|
{ |
66
|
54 |
|
return $this->data[$tableName]['row'] ?? ActiveRow::class; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Fetch selection class by table |
71
|
|
|
* @param string $tableName |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
64 |
|
public function getSelectionClass(string $tableName): string |
75
|
|
|
{ |
76
|
64 |
|
return $this->data[$tableName]['selection'] ?? Selection::class; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns all scopes registered for table |
81
|
|
|
* @param string $tableName |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
2 |
|
public function getScopes(string $tableName): array |
85
|
|
|
{ |
86
|
2 |
|
return $this->data[$tableName]['scopes'] ?? []; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns one scope |
91
|
|
|
* @param string $tableName |
92
|
|
|
* @param string $scope |
93
|
|
|
* @return Scope|null |
94
|
|
|
*/ |
95
|
6 |
|
public function getScope(string $tableName, string $scope): ?Scope |
96
|
|
|
{ |
97
|
6 |
|
return $this->data[$tableName]['scopes'][$scope] ?? null; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|