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

ResultTrait::count()   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 2
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\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
        $tableName = $this->table;
32
33 3
        if ($tableName instanceof \Rougin\Wildfire\CodeigniterModel) {
34
            $tableName = $this->table->getTableName();
35
        }
36
37 3
        $id = $this->describe->getPrimaryKey($tableName);
38
39 3
        $result = $this->query->result();
40
41 3
        foreach ($result as $row) {
42 3
            $data[$row->$id] = ucwords($row->$description);
43 3
        }
44
45 3
        return $data;
46
    }
47
48
    /**
49
     * Returns the total number of rows from the result.
50
     *
51
     * @return integer
52
     */
53
    public function count()
54
    {
55
        return $this->query->count_all_results();
56
    }
57
58
    /**
59
     * Creates an object from the specified table and row.
60
     *
61
     * @param  string  $table
62
     * @param  object  $row
63
     * @param  boolean $isForeignKey
64
     * @return array
65
     */
66
    abstract protected function createObject($table, $row, $isForeignKey = false);
67
68
    /**
69
     * Returns all rows from the specified table.
70
     *
71
     * @param  string $table
72
     * @return self
73
     */
74
    abstract public function get($table = '');
75
76
    /**
77
     * Returns the result.
78
     *
79
     * @return object
80
     */
81 15
    public function result()
82
    {
83 15
        $data = $this->getQueryResult();
84
85 15
        $result = [];
86
87 15
        if (empty($this->table)) {
88 6
            $this->get();
89 6
        }
90
91 15
        foreach ($data as $row) {
92 15
            $object = $this->createObject($this->table, $row);
93
94 15
            array_push($result, $object);
95 15
        }
96
97 15
        return $result;
98
    }
99
100
    /**
101
     * Gets the data result from the specified query.
102
     *
103
     * @return array|object
104
     */
105 15
    protected function getQueryResult()
106
    {
107 15
        $result = $this->query;
108
109 15
        if (method_exists($this->query, 'result')) {
110 15
            $result = $this->query->result();
111 15
        }
112
113 15
        return $result;
114
    }
115
}
116