Completed
Push — master ( c52182...0c0ab1 )
by Hong
02:32
created

ResultAbstract::fetchCol()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 11
rs 9.2
cc 4
eloc 7
nc 6
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver;
16
17
use Phossa2\Db\Message\Message;
18
use Phossa2\Shared\Base\ObjectAbstract;
19
use Phossa2\Db\Interfaces\ResultInterface;
20
use Phossa2\Db\Exception\RuntimeException;
21
22
/**
23
 * ResultAbstract
24
 *
25
 * @package Phossa2\Db
26
 * @author  Hong Zhang <[email protected]>
27
 * @see     ObjectAbstract
28
 * @see     ResultInterface
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 */
32
abstract class ResultAbstract extends ObjectAbstract implements ResultInterface
33
{
34
    /**
35
     * Fetched already
36
     *
37
     * @var    bool
38
     * @access protected
39
     */
40
    protected $fetched = false;
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function isSelect()/*# : bool */
46
    {
47
        return 0 !== $this->fieldCount();
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function fetchAll()/*# : array */
54
    {
55
        $this->exceptionIfNotSelect();
56
        $this->exceptionIfFetchedAlready();
57
        $this->fetched = true;
58
        return $this->realFetchAll();
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function fetchRow(/*# int */ $rowCount = 1)/*# : array */
65
    {
66
        $this->exceptionIfNotSelect();
67
        $this->exceptionIfFetchedAlready();
68
        $this->fetched = true;
69
        return $this->realFetchRow($rowCount);
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75
    public function fetchCol($col = 0, $rowCount = 0)/*# : array */
76
    {
77
        $rows = $rowCount ? $this->fetchRow($rowCount) : $this->fetchAll();
78
        $cols = [];
79
        foreach($rows as $row) {
80
            if (isset($row[$col])) {
81
                $cols[] = $row[$col];
82
            }
83
        }
84
        return $cols;
85
    }
86
87
    /**
88
     * Throw exception if not select query
89
     *
90
     * @throws RuntimeException if not a select query
91
     * @access protected
92
     */
93
    protected function exceptionIfNotSelect()
94
    {
95
        if (!$this->isSelect()) {
96
            throw new RuntimeException(
97
                Message::get(Message::DB_RESULT_NOT_SELECT),
98
                Message::DB_RESULT_NOT_SELECT
99
            );
100
        }
101
    }
102
103
    /**
104
     * Throw exception if fetched already
105
     *
106
     * @throws RuntimeException if fetched already
107
     * @access protected
108
     */
109
    protected function exceptionIfFetchedAlready()
110
    {
111
        if ($this->fetched) {
112
            throw new RuntimeException(
113
                Message::get(Message::DB_RESULT_FETCHED),
114
                Message::DB_RESULT_FETCHED
115
            );
116
        }
117
    }
118
119
    /**
120
     * Driver fetch all
121
     *
122
     * @return array
123
     * @access protected
124
     */
125
    abstract protected function realFetchAll()/*# : array */;
126
127
    /**
128
     * Driver fetch row
129
     *
130
     * @param  int $rowCount number of rows to fetch
131
     * @return array
132
     * @access protected
133
     */
134
    abstract protected function realFetchRow($rowCount)/*# : array */;
135
}
136