1 | <?php |
||
10 | class PdoReader implements CountableReader |
||
11 | { |
||
12 | /** |
||
13 | * @var \PDO |
||
14 | */ |
||
15 | protected $pdo; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $tableName; |
||
21 | |||
22 | /** |
||
23 | * @var \PDOStatement |
||
24 | */ |
||
25 | protected $statement; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $data; |
||
31 | |||
32 | /** |
||
33 | * @param \PDO $pdo |
||
34 | * @param string $sql |
||
35 | * @param array $params |
||
36 | */ |
||
37 | 4 | public function __construct(\PDO $pdo, $sql, array $params = []) |
|
38 | { |
||
39 | 4 | $this->pdo = $pdo; |
|
40 | 4 | $this->statement = $this->pdo->prepare($sql); |
|
41 | |||
42 | 4 | foreach ($params as $key => $value) { |
|
43 | $this->statement->bindValue($key, $value); |
||
44 | 4 | } |
|
45 | 4 | } |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 1 | public function getFields() |
|
51 | { |
||
52 | 1 | if ($this->statement->execute()) { |
|
53 | // Statement executed successfully |
||
54 | // Grab the first row to find keys |
||
55 | 1 | $row = $this->statement->fetch(\PDO::FETCH_ASSOC); |
|
56 | // Return field keys, or empty array no rows remain |
||
57 | 1 | return array_keys($row ? $row : []); |
|
58 | } else { |
||
59 | // If the statement errors return empty |
||
60 | return []; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 2 | public function current() |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 2 | public function next() |
|
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function key() |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 2 | public function valid() |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 2 | public function rewind() |
|
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 1 | public function count() |
|
117 | |||
118 | /** |
||
119 | * Load data if it hasn't been loaded yet |
||
120 | */ |
||
121 | 3 | protected function loadData() |
|
128 | } |
||
129 |