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

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