Passed
Push — dev_2x ( 241dab...1e248d )
by Adrian
01:59
created

MapperConfig::getQueryScopes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm;
5
6
use Sirius\Orm\Behaviour\BehaviourInterface;
7
use Sirius\Orm\Entity\GenericEntity;
8
use Sirius\Orm\Helpers\QueryHelper;
9
use Sirius\Orm\Relation\Relation;
10
11
/**
12
 * Class MapperConfig
13
 * Used to create mapper definitions that can be created using Mapper::make($mapperConfigInstance)
14
 * This is useful for dynamically generated mappers (think Wordpress custom post types)
15
 * @package Sirius\Orm
16
 */
17
class MapperConfig
18
{
19
    const ENTITY_CLASS = 'entityClass';
20
    const PRIMARY_KEY = 'primaryKey';
21
    const NAME = 'name';
22
    const TABLE = 'table';
23
    const TABLE_ALIAS = 'tableAlias';
24
    const COLUMNS = 'columns';
25
    const COLUMN_ATTRIBUTE_MAP = 'columnAttributeMap';
26
    const CASTS = 'casts';
27
    const ATTRIBUTE_DEFAULTS = 'attributeDefaults';
28
    const ENTITY_HYDRATOR = 'entityHydrator';
29
    const BEHAVIOURS = 'behaviours';
30
    const RELATIONS = 'relations';
31
    const GUARDS = 'guards';
32
33
    /**
34
     * @var string
35
     */
36
    protected $entityClass = GenericEntity::class;
37
38
    /**
39
     * @var string|array
40
     */
41
    protected $primaryKey = 'id';
42
43
    /**
44
     * @var string
45
     */
46
    protected $table;
47
48
    /**
49
     * Used in queries like so: FROM table as tableAlias
50
     * @var string
51
     */
52
    protected $tableAlias;
53
54
    /**
55
     * @var
56
     */
57
    protected $tableReference;
58
59
    /**
60
     * Table columns
61
     * @var array
62
     */
63
    protected $columns = [];
64
65
    /**
66
     * Columns casts
67
     * @var array
68
     */
69
    protected $casts = [];
70
71
    /**
72
     * Column aliases (table column => entity attribute)
73
     * @var array
74
     */
75
    protected $columnAttributeMap = [];
76
77
    /**
78
     * Default attributes
79
     * @var array
80
     */
81
    protected $attributeDefaults = [];
82
83
    /**
84
     * List of behaviours to be attached to the mapper
85
     * @var array[BehaviourInterface]
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[BehaviourInterface] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
86
     */
87
    protected $behaviours = [];
88
89
    /**
90
     * List of relations of the configured mapper
91
     * (key = name of relation, value = relation instance)
92
     * @var array|Relation[]
93
     */
94
    protected $relations = [];
95
96
    /**
97
     * List of column-value pairs that act as global filters
98
     * @var array
99
     */
100
    protected $guards = [];
101
102
    public static function fromArray(array $array)
103
    {
104
        $instance = new self;
105
        foreach ($array as $k => $v) {
106
            $instance->{$k} = $v;
107
        }
108
109
        return $instance;
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function getEntityClass(): string
116
    {
117
        return $this->entityClass;
118
    }
119
120
    /**
121
     * @return string|array
122
     */
123
    public function getPrimaryKey()
124
    {
125
        return $this->primaryKey;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getTable(): string
132
    {
133
        return $this->table;
134
    }
135
136
    /**
137
     * @param bool $fallbackToTable
138
     *
139
     * @return string
140
     */
141
    public function getTableAlias($fallbackToTable = false)
142
    {
143
        return ( ! $this->tableAlias && $fallbackToTable) ? $this->table : $this->tableAlias;
144
    }
145
146
    /**
147
     * @return array
148
     */
149
    public function getColumns(): array
150
    {
151
        return $this->columns;
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    public function getCasts(): array
158
    {
159
        return $this->casts;
160
    }
161
162
    /**
163
     * @return array
164
     */
165
    public function getColumnAttributeMap(): array
166
    {
167
        return $this->columnAttributeMap;
168
    }
169
170
171
    /**
172
     * @return array
173
     */
174
    public function getAttributeDefaults(): array
175
    {
176
        return $this->attributeDefaults;
177
    }
178
179
    /**
180
     * @return BehaviourInterface[]
181
     */
182
    public function getBehaviours(): array
183
    {
184
        return $this->behaviours;
185
    }
186
187
    /**
188
     * @return array|Relation[]
189
     */
190
    public function getRelations(): array
191
    {
192
        return $this->relations;
193
    }
194
195
    /**
196
     * @return array
197
     */
198
    public function getGuards(): array
199
    {
200
        return $this->guards;
201
    }
202
203
    public function getTableReference()
204
    {
205
        if ( ! $this->tableReference) {
206
            $this->tableReference = QueryHelper::reference($this->table, $this->tableAlias);
207
        }
208
209
        return $this->tableReference;
210
    }
211
}
212