Sqlite3PreparedStatement::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Crossjoin\Browscap\Parser\Sqlite\Adapter;
5
6
/**
7
 * Class Sqlite3PreparedStatement
8
 *
9
 * @package Crossjoin\Browscap\Parser\Sqlite\Adapter
10
 * @author Christoph Ziegenberg <[email protected]>
11
 * @link https://github.com/crossjoin/browscap
12
 */
13
class Sqlite3PreparedStatement implements PreparedStatementInterface
14
{
15
    /**
16
     * @var \SQLite3Stmt
17
     */
18
    protected $statement;
19
20
    /**
21
     * Sqlite3PreparedStatement constructor.
22
     *
23
     * @param \SQLite3Stmt $statement
24
     */
25
    public function __construct(\SQLite3Stmt $statement)
26
    {
27
        $this->setStatement($statement);
28
    }
29
30
    /**
31
     * @return \SQLite3Stmt
32
     */
33
    protected function getStatement() : \SQLite3Stmt
34
    {
35
        return $this->statement;
36
    }
37
38
    /**
39
     * @param \SQLite3Stmt $statement
40
     */
41
    protected function setStatement(\SQLite3Stmt $statement)
42
    {
43
        $this->statement = $statement;
44
    }
45
46
    /**
47
     * @param array $params
48
     *
49
     * @return array
50
     */
51
    public function execute(array $params = []) : array
52
    {
53
        foreach ($params as $paramName => $paramValue) {
54
            $type = is_int($paramValue) ? \SQLITE3_INTEGER : \SQLITE3_TEXT;
55
            $this->getStatement()->bindValue($paramName, $paramValue, $type);
56
        }
57
        $result = $this->getStatement()->execute();
58
59
        $results = [];
60
        while ($row = $result->fetchArray(\SQLITE3_ASSOC)) {
61
            $results[] = $row;
62
        }
63
64
        return $results;
65
    }
66
}
67