Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Result::getArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Result class for mysql driver
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Inji\Db\Mysql;
13
14
use Inji\Db\DriverResult;
15
16
class Result implements DriverResult {
0 ignored issues
show
Bug introduced by
There is one abstract method fetchAll in this class; you could implement it, or declare this class as abstract.
Loading history...
17
18
    public $pdoResult = null;
19
20
    public function __construct($dbResult) {
21
        $this->pdoResult = $dbResult;
22
    }
23
24
    public function getArray($keyCol = '') {
25
        $key = \Inji\App::$cur->log->start('parse result');
26
        if (!$keyCol) {
27
            return $this->pdoResult->fetchAll(\PDO::FETCH_ASSOC);
28
        }
29
        $array = [];
30
        while ($row = $this->pdoResult->fetch(\PDO::FETCH_ASSOC)) {
31
            if (isset($row[$keyCol])) {
32
                $array[$row[$keyCol]] = $row;
33
            } else {
34
                $array[] = $row;
35
            }
36
        }
37
        \Inji\App::$cur->log->end($key);
38
        return $array;
39
    }
40
41
    public function getObjects($class, $keyCol = '') {
42
        $key = \Inji\App::$cur->log->start('parse result');
43
        $array = [];
44
        while ($object = $this->pdoResult->fetchObject($class)) {
45
            if ($keyCol) {
46
                $array[$object->$keyCol] = $object;
47
            } else {
48
                $array[] = $object;
49
            }
50
        }
51
        \Inji\App::$cur->log->end($key);
52
        return $array;
53
    }
54
55
    public function fetch($className = '') {
56
        if ($className) {
57
            return $this->pdoResult->fetchObject($className);
58
        } else {
59
            return $this->pdoResult->fetch(\PDO::FETCH_ASSOC);
60
        }
61
    }
62
}