Completed
Push — master ( 145f77...f73370 )
by James Ekow Abaka
04:01
created

RecordWrapper::createNew()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * The MIT License
4
 *
5
 * Copyright 2015 ekow.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
namespace ntentan\nibii;
27
28
use ntentan\utils\Utils;
29
use ntentan\kaikai\Cache;
30
use ntentan\panie\InjectionContainer;
31
32
class RecordWrapper implements \ArrayAccess, \Countable, \Iterator
33
{
34
    use \ntentan\panie\ComponentContainerTrait;
35
    
36
    protected $hasMany = [];
37
    protected $belongsTo = [];
38
    protected $manyHaveMany = [];
39
    protected $behaviours = [];
40
    protected $table;
41
    private $modelData = [];
42
    private $invalidFields;
43
    private $dynamicOperations;
44
    private $index = 0;
45
    private $dataSet = false;
46
    protected $adapter;
47
48 36
    public function __construct(DriverAdapter $adapter)
49
    {
50 36
        $this->table = Nibii::getModelTable($this);
51 36
        $this->adapter = $adapter;
52 36
        foreach($this->behaviours as $behaviour) {
53
            $behaviourInstance = $this->getComponentInstance($behaviour);
54
            $behaviourInstance->setModel($this);
55
        }
56 36
    }
57
58
    /**
59
     *
60
     * @return \ntentan\nibii\DriverAdapter
61
     */
62
    //protected function getDataAdapter()
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
    //{
64
        
65
        /*if(!$this->adapter)
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
        {
67
            $this->adapter = DriverAdapter::getDefaultInstance();
68
        }
69
        return $this->adapter;*/
70
    //}
71
72
    /**
73
     * 
74
     * @return ModelDescription
75
     */
76 28
    public function getDescription()
77
    {
78 28
        return Cache::read(
79 28
            (new \ReflectionClass($this))->getName() . '::desc',
80 28
            function() 
81
            {
82 8
                return new ModelDescription($this);
83 28
            }
84
        );
85
    }
86
    
87 10
    public function count()
88
    {
89 10
        if ($this->dataSet) {
90 10
            return count($this->getData());
91
        } else {
92
            return $this->__call('count', []);
0 ignored issues
show
Documentation introduced by
'count' is of type string, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array() is of type array, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
        }
94
    }
95
96 12
    private function retrieveItem($key)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
97
    {
98 12
        $relationships = $this->getDescription()->getRelationships();
99 12
        if(isset($relationships[$key])) {
100 8
            return $this->fetchRelatedFields($relationships[$key]);
101
        } else {
102 4
            return isset($this->modelData[$key]) ? $this->modelData[$key] : null;
103
        }
104
    }
105
106
    /**
107
     * Create a new instance of this Model
108
     * @return \ntentan\nibii\RecordWrapper
109
     */
110 34
    public static function createNew()
111
    {
112 34
        $class = get_called_class();
113 34
        return InjectionContainer::resolve($class);
114
    }
115
116
    /**
117
     * @method
118
     * @param type $name
119
     * @param type $arguments
120
     * @return type
121
     */
122 34
    public function __call($name, $arguments)
123
    {
124 34
        if($this->dynamicOperations === null) {
125 34
            $this->dynamicOperations = new Operations($this, InjectionContainer::singleton(DriverAdapter::class));
126
        }
127 34
        return $this->dynamicOperations->perform($name, $arguments);
128
    }
129
130 26
    public static function __callStatic($name, $arguments)
131
    {
132 26
        return call_user_func_array([self::createNew(), $name], $arguments);
133
    }
134
135 8
    public function __set($name, $value)
136
    {
137 8
        $this->dataSet = true;
138 8
        $this->modelData[$name] = $value;
139 8
    }
140
141 12
    public function __get($name)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
142
    {
143 12
        return $this->retrieveItem($name);
144
    }
145
146 32
    public function getTable()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
147
    {
148 32
        return $this->table;
149
    }
150
    
151 4
    private function expandArrayValue($array, $relationships, $depth, $index = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
152
    {
153 4
        foreach($relationships as $name => $relationship) {
154 4
            $array[$name] = $this->fetchRelatedFields($relationship, $index)->toArray($depth);
155
        }
156 4
        return $array;
157
    }
158
159 16
    public function toArray($depth = 0)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
160
    {
161 16
        $relationships = $this->getDescription()->getRelationships();
162 16
        $array = $this->modelData;
163 16
        if($depth > 0) {
164 4
            if($this->hasMultipleData()) {
165
                foreach($array as $i => $value) {
166
                    $array[$i] = $this->expandArrayValue($value, $relationships, $depth - 1, $i);
167
                }
168
            } else {
169 4
                $array = $this->expandArrayValue($array, $relationships, $depth - 1);
170
            }
171
        }
172 16
        return $array;
173
    }
174
    
175 10
    public function save()
176
    {
177 10
        $return = $this->__call('save', [$this->hasMultipleData()]);
0 ignored issues
show
Documentation introduced by
'save' is of type string, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array($this->hasMultipleData()) is of type array<integer,boolean,{"0":"boolean"}>, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178 10
        $this->invalidFields = $this->dynamicOperations->getInvalidFields();
179 10
        return $return;
180
    }
181
    
182
    public function isValid()
183
    {
184
        return $this->__call('isValid', []);
0 ignored issues
show
Documentation introduced by
'isValid' is of type string, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
array() is of type array, but the function expects a object<ntentan\nibii\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
185
    }
186
187 18
    private function hasMultipleData()
188
    {
189 18
        if(count($this->modelData) > 0) {
190 16
            return is_numeric(array_keys($this->modelData)[0]);
191
        } else {
192 2
            return false;
193
        }
194
    }
195
196 14
    public function getData()
197
    {
198 14
        $data = [];
199
                
200 14
        if(count($this->modelData) == 0) {
201 6
            $data = $this->modelData;
202 12
        } else if($this->hasMultipleData()) {
203 2
            $data = $this->modelData;
204 12
        } else if(count($this->modelData) > 0) {
205 12
            $data[] = $this->modelData;
206
        }
207
        
208 14
        return $data;
209
    }
210
211 26
    public function setData($data)
212
    {
213 26
        $this->dataSet = true;
214 26
        $this->modelData = $data;
215 26
    }
216
    
217
    public function mergeData($data)
218
    {
219
        foreach($data as $key => $value) {
220
            $this->modelData[$key] = $value;
221
        }
222
        $this->dataSet = true;
223
    }
224
225 2
    public function offsetExists($offset)
226
    {
227 2
        return isset($this->modelData[$offset]);
228
    }
229
230 2
    public function offsetGet($offset)
231
    {
232 2
        if (is_numeric($offset)) {
233 2
            return $this->wrap($offset);
234
        } else {
235 2
            return $this->retrieveItem($offset);
236
        }
237
    }
238
239 2
    public function offsetSet($offset, $value)
240
    {
241 2
        $this->dataSet = true;
242 2
        $this->modelData[$offset] = $value;
243 2
    }
244
245
    public function offsetUnset($offset)
246
    {
247
        unset($this->modelData[$offset]);
248
    }
249
250 6
    private function wrap($offset)
251
    {
252 6
        if(isset($this->modelData[$offset])) {
253 6
            $newInstance = $this->createNew();
254 6
            $newInstance->setData($this->modelData[$offset]);
255 6
            return $newInstance;
256
        } else {
257
            return null;
258
        }
259
    }
260
261 4
    public function getInvalidFields()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
262
    {
263 4
        return $this->invalidFields;
264
    }
265
266
    public function getHasMany()
267
    {
268
        return $this->hasMany;
269
    }
270
271
    public function getBelongsTo()
272
    {
273
        return $this->belongsTo;
274
    }
275
276 4
    public function current()
277
    {
278 4
        return $this->wrap($this->index);
279
    }
280
281
    public function key()
282
    {
283
        return $this->index;
284
    }
285
286 4
    public function next()
287
    {
288 4
        $this->index++;
289 4
    }
290
291 4
    public function rewind()
292
    {
293 4
        $this->index = 0;
294 4
    }
295
296 4
    public function valid()
297
    {
298 4
        return isset($this->modelData[$this->index]);
299
    }
300
301 12
    private function fetchRelatedFields($relationship, $index = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
302
    {
303 12
        if($index === null) {
304 12
            $data = $this->modelData;
305
        } else {
306
            $data = $this->modelData[$index];
307
        }
308 12
        $model = $relationship->getModelInstance();
309 12
        if(empty($data)) {
310
            return $model;
311
        } else {
312 12
            return $model->fetch($relationship->getQuery($data));
313
        }
314
    }
315
316 8
    public function getRelationships()
317
    {
318
        return [
319 8
            'HasMany' => $this->hasMany,
320 8
            'BelongsTo' => $this->belongsTo,
321 8
            'ManyHaveMany' => $this->manyHaveMany
322
        ];
323
    }
324
    
325
    public function usetField($field)
326
    {
327
        unset($this->modelData[$field]);
328
    }
329
    
330 8
    public function preSaveCallback()
331
    {
332
        
333 8
    }
334
    
335 4
    public function postSaveCallback($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
336
    {
337
        
338 4
    }
339
    
340 2
    public function preUpdateCallback()
341
    {
342
        
343 2
    }
344
    
345 2
    public function postUpdateCallback()
346
    {
347
        
348 2
    }
349
    
350 10
    public function getBehaviours()
351
    {
352 10
        return $this->loadedComponents;
353
    }
354
}
355