Completed
Pull Request — master (#10)
by Samuel
05:07 queued 03:05
created

ActiveRow::offsetSet()   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.037

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1.037
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleMapper;
6
7
use Nette\Database\Table\Selection as NetteDatabaseSelection;
8
use Nette\Database\Table\ActiveRow as NetteDatabaseActiveRow;
9
use ArrayIterator;
10
use IteratorAggregate;
11
use Nette\Database\Table\IRow;
12
use Nette\DeprecatedException;
13
use SimpleMapper\Exception\ActiveRowException;
14
use SimpleMapper\Structure\Structure;
15
16
class ActiveRow implements IteratorAggregate, IRow
17
{
18
    /** @var NetteDatabaseActiveRow */
19
    protected $record;
20
21
    /** @var Structure */
22
    protected $structure;
23
24
    /**
25
     * @param NetteDatabaseActiveRow $record
26
     * @param Structure $structure
27
     */
28 52
    public function __construct(NetteDatabaseActiveRow $record, Structure $structure)
29
    {
30 52
        $this->record = $record;
31 52
        $this->structure = $structure;
32 52
    }
33
34
    /**
35
     * @param string $name
36
     * @return mixed|ActiveRow
37
     */
38 16
    public function __get($name)
39
    {
40 16
        $result = $this->record->$name;
41 14
        return $result instanceof IRow ? $this->prepareRecord($result) : $result;
42
    }
43
44
    /**
45
     * @return NetteDatabaseActiveRow
46
     */
47 2
    public function getRecord(): NetteDatabaseActiveRow
48
    {
49 2
        return $this->record;
50
    }
51
52
    /**
53
     * @return NetteDatabaseSelection
54
     */
55 2
    public function getTable(): NetteDatabaseSelection
56
    {
57 2
        return $this->record->getTable();
58
    }
59
60
    /**
61
     * @param NetteDatabaseSelection $selection
62
     * @throws ActiveRowException
63
     */
64 2
    public function setTable(NetteDatabaseSelection $selection): void
65
    {
66 2
        throw new ActiveRowException('Internal IRow interface method');
67
    }
68
69
    /**********************************************************************\
70
     * Wrapper function
71
    \**********************************************************************/
72
73
    /**
74
     * @return string
75
     */
76 2
    public function __toString(): string
77
    {
78 2
        return (string) $this->record;
79
    }
80
81
    /**
82
     * @return array
83
     */
84 6
    public function toArray(): array
85
    {
86 6
        return $this->record->toArray();
87
    }
88
89
    /**
90
     * @param bool|true $need
91
     * @return mixed
92
     */
93 2
    public function getPrimary($need = true)
94
    {
95 2
        return $this->record->getPrimary($need);
96
    }
97
98
    /**
99
     * @param bool|true $need
100
     * @return string
101
     */
102 2
    public function getSignature($need = true): string
103
    {
104 2
        return $this->record->getSignature($need);
105
    }
106
107
    /**
108
     * @param array|\Traversable $data
109
     * @return bool
110
     */
111 6
    public function update($data): bool
112
    {
113 6
        return $this->record->update($data);
114
    }
115
116
    /**
117
     * @return int
118
     */
119 4
    public function delete(): int
120
    {
121 4
        return $this->record->delete();
122
    }
123
124
    /**
125
     * Returns referenced row
126
     * @param string $key
127
     * @param string $throughColumn
128
     * @return ActiveRow|null
129
     */
130 6
    public function ref($key, $throughColumn = null): ?ActiveRow
131
    {
132 6
        $row = $this->record->ref($key, $throughColumn);
133 6
        return $row instanceof IRow ? $this->prepareRecord($row) : null;
134
    }
135
136
137
    /**
138
     * Returns referencing rows
139
     * @param string $key
140
     * @param string $throughColumn
141
     * @return Selection
142
     */
143 4
    public function related($key, $throughColumn = null): ?Selection
144
    {
145 4
        $selection = $this->record->related($key, $throughColumn);
146 4
        return $selection instanceof NetteDatabaseSelection ? $this->prepareSelection($selection) : null;
147
    }
148
149
    /**********************************************************************\
150
     * IteratorAggregate interface
151
    \**********************************************************************/
152
153
    /**
154
     * @return ArrayIterator
155
     */
156 2
    public function getIterator(): ArrayIterator
157
    {
158 2
        return $this->record->getIterator();
159
    }
160
161
    /**********************************************************************\
162
     * ArrayAccess interface
163
    \**********************************************************************/
164
165
    /**
166
     * Returns value of column
167
     * @param string $key  column name
168
     * @return mixed
169
     */
170 8
    public function offsetGet($key)
171
    {
172 8
        $result = $this->record->offsetGet($key);
173 6
        return $result instanceof IRow ? $this->prepareRecord($result) : $result;
174
    }
175
176
    /**
177
     * Tests if column exists
178
     * @param string $key   column name
179
     * @return bool
180
     */
181 2
    public function offsetExists($key): bool
182
    {
183 2
        return $this->record->offsetExists($key);
184
    }
185
186
    /**
187
     * Stores value in column
188
     * @param string $key   column name
189
     * @param string $value
190
     * @throws DeprecatedException
191
     */
192 2
    public function offsetSet($key, $value): void
193
    {
194 2
        $this->record->offsetSet($key, $value);
195
    }
196
197
    /**
198
     * Removes column from data
199
     * @param string $key column name
200
     * @throws DeprecatedException
201
     */
202 2
    public function offsetUnset($key): void
203
    {
204 2
        $this->record->offsetUnset($key);
205
    }
206
207
    /**********************************************************************\
208
     * Extend methods
209
    \**********************************************************************/
210
211
    /**
212
     * Returns mm referencing rows
213
     * @param Selection $selection
214
     * @param string $ref
215
     * @param string $refPrimary
216
     * @return array
217
     */
218 4
    protected function mmRelated(Selection $selection, string $ref, string $refPrimary = 'id'): array
219
    {
220 4
        $result = [];
221 4
        foreach ($selection as $row) {
222 4
            $result[$row->ref($ref)->$refPrimary] = $row->ref($ref);
223
        }
224 4
        return $result;
225
    }
226
227
    /**********************************************************************\
228
     * Build methods
229
    \**********************************************************************/
230
231
    /**
232
     * Prepare one record
233
     * @param IRow $row
234
     * @return ActiveRow
235
     */
236 10
    protected function prepareRecord(IRow $row): ActiveRow
237
    {
238 10
        $recordClass = $this->structure->getActiveRowClass($row->getTable()->getName());
239 10
        return new $recordClass($row, $this->structure);
240
    }
241
242
    /**
243
     * Prepare selection
244
     * @param NetteDatabaseSelection $selection
245
     * @return Selection
246
     */
247 4
    protected function prepareSelection(NetteDatabaseSelection $selection): Selection
248
    {
249 4
        $selectionClass = $this->structure->getSelectionClass($selection->getName());
250 4
        return new $selectionClass($selection, $this->structure);
251
    }
252
}
253