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

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