Passed
Push — master ( e728fc...f58bd0 )
by Rougin
01:40
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
    public function __construct(array $attributes = array())
61
    {
62
        $this->attributes = $this->original = $attributes;
63
64
        $keys = (array) array_keys($this->original);
65
66
        $this->visible = $this->visible ?: (array) $keys;
67
    }
68
69
    /**
70
     * Returns the attribute or the __get from \CI_Model.
71
     *
72
     * @param  string $key
73
     * @return mixed
74
     */
75
    public function __get($key)
76
    {
77
        if (isset($this->attributes[$key]) === true) {
78
            $value = $this->attributes[(string) $key];
79
80
            $method = 'get_' . $key . '_attribute';
81
82
            $exists = method_exists($this, $method);
83
84
            return $exists ? $this->{$method}() : $value;
85
        }
86
87
        return parent::__get((string) $key);
88
    }
89
90
    /**
91
     * Returns the model as a JSON string.
92
     *
93
     * @return string
94
     */
95
    public function __toString()
96
    {
97
        return json_encode($this->array());
98
    }
99
100
    /**
101
     * Returns the attributes as an array.
102
     *
103
     * @return array
104
     */
105
    public function array()
106
    {
107
        $flipped = array_flip((array) $this->columns());
108
109
        $values = (array) $this->attributes;
110
111
        return array_intersect_key($values, $flipped);
112
    }
113
114
    /**
115
     * Returns an array of column names.
116
     *
117
     * @return array
118
     */
119
    public function columns()
120
    {
121
        return array_diff($this->visible, $this->hidden);
122
    }
123
124
    /**
125
     * Returns the primary key.
126
     *
127
     * @return string
128
     */
129
    public function primary()
130
    {
131
        return $this->primary;
132
    }
133
}
134