Completed
Push — master ( 6e0700...30b471 )
by Todd
02:33
created

FetchModeTrait::getFetchMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

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 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Db\Utils;
9
10
use PDO;
11
12
trait FetchModeTrait {
13
    /**
14
     * @var array The fetch arguments (if any).
15
     */
16
    private $fetchArgs = [PDO::FETCH_ASSOC];
17
18
    /**
19
     * Get the default fetch mode.
20
     *
21
     * @return int Returns the current fetch mode.
22
     */
23 17
    public function getFetchMode() {
24 17
        return reset($this->fetchArgs);
25
    }
26
27
    /**
28
     * Get the fetch arguments.
29
     *
30
     * @return array Returns an array of arguments including the fetch mode.
31
     */
32 83
    public function getFetchArgs() {
33 83
        return $this->fetchArgs;
34
    }
35
36
    /**
37
     * Set the default fetch mode.
38
     *
39
     * @param int|string $mode One of the **PDO::FETCH_*** constants or a class name.
40
     * @param array $args Additional arguments for {@link \PDOStatement::fetchAll()}.
41
     * @return $this
42
     * @see http://php.net/manual/en/pdostatement.fetchall.php
43
     */
44 17
    public function setFetchMode($mode, ...$args) {
45 17
        if (is_string($mode)) {
46
            array_unshift($args, PDO::$mode);
47
            $mode = PDO::FETCH_CLASS;
48
        }
49 17
        $this->fetchArgs = array_merge([$mode], $args);
50
51 17
        return $this;
52
    }
53
}
54