Completed
Pull Request — master (#9)
by Samuel
03:17
created

BaseStructure::getSelectionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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