Completed
Pull Request — master (#10)
by Rougin
02:32
created

ResultTrait::resetQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Rougin\Wildfire\Traits;
4
5
/**
6
 * Result Trait
7
 *
8
 * @package Wildfire
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 *
11
 * @property \Rougin\Describe\Describe $describe
12
 * @property \CI_DB_null               $query
13
 */
14
trait ResultTrait
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $table = '';
20
21
    /**
22
     * Lists all data in dropdown format.
23
     *
24
     * @param  string $description
25
     * @return array
26
     */
27 3
    public function asDropdown($description = 'description')
28
    {
29 3
        $data = [];
30
31 3
        $id = $this->describe->getPrimaryKey($this->table);
32
33 3
        $result = $this->query->result();
34
35 3
        foreach ($result as $row) {
36 3
            $data[$row->$id] = ucwords($row->$description);
37 3
        }
38
39 3
        $this->resetQuery();
40
41 3
        return $data;
42
    }
43
44
    /**
45
     * Creates an object from the specified table and row.
46
     *
47
     * @param  string $table
48
     * @param  object $row
49
     * @return array
50
     */
51
    abstract protected function createObject($table, $row);
52
53
    /**
54
     * Returns all rows from the specified table.
55
     *
56
     * @param  string $table
57
     * @return self
58
     */
59
    abstract public function get($table = '');
60
61
    /**
62
     * Returns the result.
63
     *
64
     * @return object
65
     */
66 24
    public function result()
67
    {
68 24
        $data = $this->getQueryResult();
69
70 24
        $result = [];
71
72 24
        if (empty($this->table)) {
73 6
            $this->get();
74 6
        }
75
76 24
        foreach ($data as $row) {
77 21
            $object = $this->createObject($this->table, $row);
78
79 21
            array_push($result, $object);
80 24
        }
81
82 24
        $this->resetQuery();
83
84 24
        return $result;
85
    }
86
87
    /**
88
     * Gets the data result from the specified query.
89
     *
90
     * @return array|object
91
     */
92 24
    protected function getQueryResult()
93
    {
94 24
        $result = $this->query;
95
96 24
        if (method_exists($this->query, 'result')) {
97 24
            $result = $this->query->result();
98 24
        }
99
100 24
        if ($this->table) {
101 18
            $this->get($this->table);
102 18
        }
103
104 24
        return $result;
105
    }
106
107
    /**
108
     * Resets the entire query and table name.
109
     *
110
     * @return void
111
     */
112 27
    protected function resetQuery()
113
    {
114 27
        $this->table = null;
115 27
        $this->query = null;
116 27
    }
117
}
118