Passed
Push — master ( f58bd0...03116c )
by Rougin
01:39
created

Model::array()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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 hidden for serialization.
22
     *
23
     * @var array
24
     */
25
    protected $hidden = array();
26
27
    /**
28
     * The primary key for the model.
29
     *
30
     * @var string
31
     */
32
    protected $primary = 'id';
33
34
    /**
35
     * The model attribute's original state.
36
     *
37
     * @var array
38
     */
39
    protected $original = array();
40
41
    /**
42
     * The table associated with the model.
43
     *
44
     * @var string
45
     */
46
    protected $table = '';
47
48
    /**
49
     * The attributes that should be visible for serialization.
50
     *
51
     * @var array
52
     */
53
    protected $visible = array();
54
55
    /**
56
     * Initializes the model instance.
57
     *
58
     * @param array $attributes
59
     */
60 30
    public function __construct(array $attributes = array())
61
    {
62 30
        $this->attributes = $this->original = $attributes;
63
64 30
        $keys = (array) array_keys($this->original);
65
66 30
        $this->visible = $this->visible ?: (array) $keys;
67 30
    }
68
69
    /**
70
     * Returns the attribute or the __get from \CI_Model.
71
     *
72
     * @param  string $key
73
     * @return mixed
74
     */
75 15
    public function __get($key)
76
    {
77 15
        if (isset($this->attributes[$key]) === true) {
78 6
            $value = $this->attributes[(string) $key];
79
80 6
            $method = 'get_' . $key . '_attribute';
81
82 6
            $exists = method_exists($this, $method);
83
84 6
            return $exists ? $this->{$method}() : $value;
85
        }
86
87 9
        return parent::__get((string) $key);
88
    }
89
90
    /**
91
     * Returns the model as a JSON string.
92
     *
93
     * @return string
94
     */
95 3
    public function __toString()
96
    {
97 3
        return json_encode($this->data());
98
    }
99
100
    /**
101
     * Returns an array of column names.
102
     *
103
     * @return array
104
     */
105 3
    public function columns()
106
    {
107 3
        return array_diff($this->visible, $this->hidden);
108
    }
109
110
    /**
111
     * Returns the attributes as an array.
112
     *
113
     * @return array
114
     */
115 3
    public function data()
116
    {
117 3
        $flipped = array_flip((array) $this->columns());
118
119 3
        $values = (array) $this->attributes;
120
121 3
        return array_intersect_key($values, $flipped);
122
    }
123
124
    /**
125
     * Returns the primary key.
126
     *
127
     * @return string
128
     */
129 6
    public function primary()
130
    {
131 6
        return $this->primary;
132
    }
133
}
134