Completed
Push — 2.0 ( 1160ec...acba87 )
by Vermeulen
05:15
created

Select::fetchRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BfwSql\Executers;
4
5
use \PDO;
6
7
class Select extends Common
8
{
9
    /**
10
     * @var string $returnType PHP Type used for return result
11
     */
12
    protected $returnType = '';
13
    
14
    /**
15
     * Getter accessor to returnType property
16
     * 
17
     * @return string
18
     */
19
    public function getReturnType(): string
20
    {
21
        return $this->returnType;
22
    }
23
    
24
    /**
25
     * Setter accessor to property returnType
26
     * 
27
     * @param string $returnType The new return type value
28
     * 
29
     * @return $this
30
     */
31
    public function setReturnType(string $returnType): self
32
    {
33
        $this->returnType = $returnType;
34
        return $this;
35
    }
36
    
37
    /**
38
     * Return the PDO constant for the returnType declared
39
     * 
40
     * @return integer
41
     */
42
    protected function obtainPdoFetchType(): int
43
    {
44
        if ($this->returnType === 'object') {
45
            return PDO::FETCH_OBJ;
46
        }
47
        
48
        return PDO::FETCH_ASSOC;
49
    }
50
    
51
    /**
52
     * Fetch one row of the result
53
     * 
54
     * @return mixed
55
     */
56
    public function fetchRow()
57
    {
58
        $req = $this->execute();
59
        return $req->fetch($this->obtainPdoFetchType());
60
    }
61
    
62
    /**
63
     * Fetch all rows returned by the request
64
     * 
65
     * @return \Generator
66
     */
67
    public function fetchAll(): \Generator
68
    {
69
        $request = $this->execute(); //throw an Exception if error
70
        
71
        while ($row = $request->fetch($this->obtainPdoFetchType())) {
72
            yield $row;
73
        }
74
    }
75
}
76