Completed
Push — master ( 41c061...7c306b )
by Samuel
9s
created

ActiveRow::setTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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