Model::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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