Completed
Pull Request — master (#9)
by Samuel
15:22
created

CustomStructure::getScopes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $activeRowClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
27
            $this->data[$table]['row'] = $activeRowClass;
28
        }
29
30
        if ($selectionClass) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $selectionClass of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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