Passed
Push — master ( d86cd8...cd9827 )
by Rougin
02:47
created

Model::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 1
crap 3
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
/**
6
 * Model
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 */
11
class Model extends \CI_Model
12
{
13
    /**
14
     * The model's attributes.
15
     *
16
     * @var array
17
     */
18
    protected $attributes = array();
19
20
    /**
21
     * The attributes that should be cast to native types.
22
     *
23
     * @var array
24
     */
25
    protected $casts = array('id' => 'integer');
26
27
    /**
28
     * The attributes that should be hidden for serialization.
29
     *
30
     * @var array
31
     */
32
    protected $hidden = array();
33
34
    /**
35
     * The primary key for the model.
36
     *
37
     * @var string
38
     */
39
    protected $primary = 'id';
40
41
    /**
42
     * The model attribute's original state.
43
     *
44
     * @var array
45
     */
46
    protected $original = array();
47
48
    /**
49
     * The table associated with the model.
50
     *
51
     * @var string
52
     */
53
    protected $table = '';
54
55
    /**
56
     * The attributes that should be visible for serialization.
57
     *
58
     * @var array
59
     */
60
    protected $visible = array();
61
62
    /**
63
     * Initializes the model instance.
64
     *
65
     * @param array $attributes
66
     */
67 36
    public function __construct(array $attributes = array())
68
    {
69 36
        $casts = (array) array('id' => 'integer');
70
71 36
        $this->casts = array_merge($casts, $this->casts);
72
73 36
        $this->original = (array) $attributes;
74
75 36
        foreach ($attributes as $key => $value) {
76 33
            $casted = $this->cast($key, $value);
77
78 33
            $this->attributes[$key] = $casted;
79 24
        }
80
81 36
        $keys = array_keys($this->original);
82
83 36
        $this->visible = $this->visible ?: $keys;
84 36
    }
85
86
    /**
87
     * Returns the attribute or from \CI_Model::__get.
88
     *
89
     * @param  string $key
90
     * @return mixed
91
     */
92 15
    public function __get($key)
93
    {
94 15
        if (isset($this->attributes[$key]) === true) {
95 6
            $value = $this->attributes[(string) $key];
96
97 6
            $method = 'get_' . $key . '_attribute';
98
99 6
            $exists = method_exists($this, $method);
100
101 6
            return $exists ? $this->{$method}() : $value;
102
        }
103
104 9
        return parent::__get((string) $key);
105
    }
106
107
    /**
108
     * Returns the model as a JSON string.
109
     *
110
     * @return string
111
     */
112 3
    public function __toString()
113
    {
114 3
        return json_encode($this->data());
115
    }
116
117
    /**
118
     * Returns an array of column names.
119
     *
120
     * @return array
121
     */
122 9
    public function columns()
123
    {
124 9
        return array_diff($this->visible, $this->hidden);
125
    }
126
127
    /**
128
     * Returns the attributes as an array.
129
     *
130
     * @return array
131
     */
132 9
    public function data()
133
    {
134 9
        $flipped = array_flip((array) $this->columns());
135
136 9
        $values = (array) $this->attributes;
137
138 9
        return array_intersect_key($values, $flipped);
139
    }
140
141
    /**
142
     * Returns the primary key.
143
     *
144
     * @return string
145
     */
146 6
    public function primary()
147
    {
148 6
        return $this->primary;
149
    }
150
151
    /**
152
     * Casts an attribute to a native PHP type.
153
     *
154
     * @param  string $key
155
     * @param  mixed  $value
156
     * @return mixed
157
     */
158 33
    protected function cast($key, $value)
159
    {
160 33
        $exists = isset($this->casts[$key]);
161
162 33
        $type = trim(strtolower((string) ''));
163
164 33
        $exists && $type = $this->casts[$key];
165
166 33
        switch (trim(strtolower($type))) {
167 33
            case 'boolean':
168 18
                return (boolean) $value;
169 33
            case 'integer':
170 21
                return (integer) $value;
171 20
            default:
172 30
                return $value;
173 20
        }
174
    }
175
}
176