Completed
Push — master ( 6067eb...3f355c )
by Samuel
15:57 queued 01:01
created

BaseStructure::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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