Passed
Push — master ( 2ba09c...157135 )
by Rougin
02:07
created

Model::table()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 45
    public function __construct(array $attributes = array())
68
    {
69 45
        $casts = (array) array('id' => 'integer');
70
71 45
        $this->casts = array_merge($casts, $this->casts);
72
73 45
        $this->original = (array) $attributes;
74
75 45
        foreach ($attributes as $key => $value) {
76 42
            $casted = $this->cast($key, $value);
77
78 42
            $this->attributes[$key] = $casted;
79 45
        }
80
81 45
        $keys = array_keys($this->original);
82
83 45
        $this->visible = $this->visible ?: $keys;
84 45
    }
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 9
    public function primary()
147
    {
148 9
        return $this->primary;
149
    }
150
151
    /**
152
     * Returns the name of the table.
153
     *
154
     * @return string
155
     */
156 9
    public function table()
157
    {
158 9
        return $this->table;
159
    }
160
161
    /**
162
     * Casts an attribute to a native PHP type.
163
     *
164
     * @param  string $key
165
     * @param  mixed  $value
166
     * @return mixed
167
     */
168 42
    protected function cast($key, $value)
169
    {
170 42
        $exists = isset($this->casts[$key]);
171
172 42
        $type = trim(strtolower((string) ''));
173
174 42
        $exists && $type = $this->casts[$key];
175
176 42
        switch (trim(strtolower($type))) {
177 42
            case 'boolean':
178 27
                return (boolean) $value;
179 42
            case 'integer':
180 30
                return (integer) $value;
181 39
            default:
182 39
                return $value;
183 39
        }
184
    }
185
}
186