Completed
Pull Request — master (#1)
by Rougin
04:25
created

CodeigniterModel::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
/**
6
 * Codeigniter Model
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 */
11
class CodeigniterModel extends \CI_Model
12
{
13
    /**
14
     * Defines an inverse one-to-one or many relationship.
15
     *
16
     * @var array
17
     */
18
    protected $_belongs_to = [];
19
20
    /**
21
     * Columns that will be displayed.
22
     * If not set, it will get the columns from the database table.
23
     *
24
     * @var array
25
     */
26
    protected $_columns = [];
27
28
    /**
29
     * Columns that will be hidden in the display.
30
     * If not set, it will hide a "password" column if it exists.
31
     *
32
     * @var array
33
     */
34
    protected $_hidden = [ 'password' ];
35
36
    /**
37
     * The table associated with the model.
38
     *
39
     * @var string
40
     */
41
    protected $_table = '';
42
43
    /**
44
     * @var \Rougin\Wildfire\Wildfire
45
     */
46
    private $_wildfire;
47
48
    /**
49
     * @var array
50
     */
51
    private $_with = [];
52
53
    public function __construct()
54
    {
55
        parent::__construct();
56
57
        $this->load->database();
0 ignored issues
show
Documentation introduced by
The property load does not exist on object<Rougin\Wildfire\CodeigniterModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
59
        $this->_wildfire = new Wildfire($this->db);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Rougin\Wildfire\CodeigniterModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
    }
61
62
    /**
63
     * Returns all of the models from the database.
64
     *
65
     * @return array
66
     */
67
    public function all()
68
    {
69
        return $this->findBy([]);
70
    }
71
72
    /**
73
     * Deletes the specified ID of the model from the database.
74
     *
75
     * @param  integer $id
76
     * @return void
77
     */
78
    public function delete($id)
79
    {
80
        return $this->_wildfire->delete($this, $id);
81
    }
82
83
    /**
84
     * Finds the specified model from the database.
85
     *
86
     * @param  integer $id
87
     * @return mixed
88
     */
89
    public function find($id)
90
    {
91
        return $this->_wildfire->find($this, $id);
92
    }
93
94
    /**
95
     * Finds the specified model from the database by the given delimiters.
96
     *
97
     * @param  array $delimiters
98
     * @return mixed
99
     */
100
    public function findBy(array $delimiters)
101
    {
102
        $this->db->where($delimiters);
0 ignored issues
show
Documentation introduced by
The property db does not exist on object<Rougin\Wildfire\CodeigniterModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
104
        return $this->get()->result();
105
    }
106
107
    /**
108
     * Returns all rows from the specified table.
109
     *
110
     * @return self
111
     */
112
    public function get()
113
    {
114
        return $this->_wildfire->get($this);
115
    }
116
117
    /**
118
     * Returns "belongs to" relationships.
119
     *
120
     * @return
121
     */
122
    public function getBelongsToRelationships()
123
    {
124
        return $this->_belongs_to;
125
    }
126
127
    /**
128
     * Returns the specified columns of the model.
129
     *
130
     * @return array
131
     */
132
    public function getColumns()
133
    {
134
        return $this->_columns;
135
    }
136
137
    /**
138
     * Returns the specified hidden columns of the model.
139
     *
140
     * @return array
141
     */
142
    public function getHiddenColumns()
143
    {
144
        return $this->_hidden;
145
    }
146
147
    /**
148
     * Gets the defined relationships.
149
     *
150
     * @return
151
     */
152
    public function getRelationships()
153
    {
154
        return $this->_with;
155
    }
156
157
    /**
158
     * Gets the specified table name of the model.
159
     *
160
     * @return string
161
     */
162
    public function getTableName()
163
    {
164
        if (! $this->_table) {
165
            return plural(strtolower(get_class($this)));
166
        }
167
168
        return $this->_table;
169
    }
170
171
    /**
172
     * Adds a relationship/s to the model.
173
     *
174
     * @param  string|array $relationships
175
     * @return self
176
     */
177
    public function with($relationships)
178
    {
179
        if (is_string($relationships)) {
180
            $relationships = [ $relationships ];
181
        }
182
183
        foreach ($relationships as $relationship) {
184
            array_push($this->_with, $relationship);
185
        }
186
187
        return $this;
188
    }
189
190
    /**
191
     * Calls methods from this class in underscore case.
192
     *
193
     * @param  string $method
194
     * @param  mixed  $parameters
195
     * @return mixed
196
     */
197 View Code Duplication
    public function __call($method, $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
198
    {
199
        $method = camelize($method);
200
        $result = $this;
201
202
        if (method_exists($this, $method)) {
203
            $class = [$this, $method];
204
            
205
            $result = call_user_func_array($class, $parameters);
206
        }
207
208
        return $result;
209
    }
210
}
211