Wildfire::json()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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