Passed
Push — master ( d86cd8...cd9827 )
by Rougin
02:47
created

Wildfire::data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Rougin\Wildfire;
4
5
use CI_DB_query_builder as QueryBuilder;
6
use CI_DB_result as QueryResult;
7
8
/**
9
 * Wildfire
10
 *
11
 * @package Wildfire
12
 * @author  Rougin Royce Gutib <[email protected]>
13
 */
14
class Wildfire
15
{
16
    /**
17
     * @var \CI_DB_query_builder
18
     */
19
    protected $builder;
20
21
    /**
22
     * @var \CI_DB_result
23
     */
24
    protected $result;
25
26
    /**
27
     * @var string
28
     */
29
    protected $table = '';
30
31
    /**
32
     * Calls a method from the \CI_DB_query_builder instance.
33
     *
34
     * @param  string $method
35
     * @param  array  $arguments
36
     * @return self
37
     * @throws \BadMethodCallException
38
     */
39 6
    public function __call($method, $arguments)
40
    {
41 6
        if (method_exists($this->builder, $method) === true) {
42 3
            $instance = (array) array($this->builder, (string) $method);
43
44 3
            $this->builder = call_user_func_array($instance, $arguments);
45
46 3
            return $this;
47
        }
48
49 3
        $method = get_class($this) . '::' . $method . '()';
50
51 3
        $message = 'Call to undefined method ' . $method;
52
53 3
        throw new \BadMethodCallException((string) $message);
54
    }
55
56
    /**
57
     * Initializes the Wildfire instance.
58
     *
59
     * @param \CI_DB_query_builder|\CI_DB_result $cidb
60
     */
61 24
    public function __construct($cidb)
62
    {
63 24
        $builder = $cidb instanceof QueryBuilder;
64
65 24
        $builder === true && $this->builder = $cidb;
66
67 24
        $result = $cidb instanceof QueryResult;
68
69 24
        $result === true && $this->result = $cidb;
70 24
    }
71
72
    /**
73
     * Returns result data in array dropdown format.
74
     *
75
     * @param  string $column
76
     * @return array
77
     */
78 3
    public function dropdown($column)
79
    {
80 3
        $data = array();
81
82 3
        foreach ($this->result() as $item) {
83 3
            $text = ucwords($item->$column);
84
85 3
            $id = $item->{$item->primary()};
86
87 3
            $data[$id] = (string) $text;
88 2
        }
89
90 3
        return $data;
91
    }
92
93
    /**
94
     * Finds the row from storage based on given identifier.
95
     *
96
     * @param  string  $table
97
     * @param  integer $id
98
     * @return \Rougin\Wildfire\Model
99
     */
100 3
    public function find($table, $id)
101
    {
102 3
        $singular = ucwords(singular($table));
103
104 3
        $model = new $singular;
105
106 3
        $data = array($model->primary() => $id);
107
108 3
        $this->builder->where((array) $data);
109
110 3
        $items = $this->get($table)->result();
111
112 3
        return current((array) $items);
113
    }
114
115
    /**
116
     * Returns an array of rows from a specified table.
117
     *
118
     * @param  string       $table
119
     * @param  integer|null $limit
120
     * @param  integer|null $offset
121
     * @return self
122
     */
123 18
    public function get($table = '', $limit = null, $offset = null)
124
    {
125 18
        $this->table = (string) ucwords((string) singular($table));
126
127 18
        $this->result = $this->builder->get($table, $limit, $offset);
128
129 18
        return $this;
130
    }
131
132
    /**
133
     * Returns the result with model instances.
134
     *
135
     * @param  string $model
136
     * @return \Rougin\Wildfire\Model[]
137
     */
138 21
    public function result($model = '')
139
    {
140 21
        $singular = ucwords(singular($this->table));
141
142 21
        $model = $model === '' ? $singular : $model;
143
144 21
        $items = $this->result->result_array();
145
146 21
        $length = (integer) count($items);
147
148 21
        for ($i = 0; $i < (integer) $length; $i++) {
149 21
            $item = (array) $items[(integer) $i];
150
151 21
            $items[$i] = new $model((array) $item);
152 14
        }
153
154 21
        return (array) $items;
155
    }
156
157
    /**
158
     * Returns the result as a JSON string.
159
     *
160
     * @param  string $model
161
     * @return string
162
     */
163 3
    public function json($model = '')
164
    {
165 3
        return json_encode($this->data($model));
166
    }
167
168
    /**
169
     * Returns the result in array format.
170
     *
171
     * @param  string $model
172
     * @return array
173
     */
174 6
    public function data($model = '')
175
    {
176 6
        $items = $this->result((string) $model);
177
178 6
        $length = (integer) count($items);
179
180 6
        for ($i = 0; $i < $length; $i++) {
181 6
            $data = $items[$i]->data();
182
183 6
            $items[$i] = (array) $data;
184 4
        }
185
186 6
        return $items;
187
    }
188
}
189