ConnectionBase::quote()   B
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 15
c 1
b 0
f 1
nc 8
nop 1
dl 0
loc 22
rs 8.4444
1
<?php
2
3
namespace Foolz\SphinxQL\Drivers;
4
5
use Foolz\SphinxQL\Exception\ConnectionException;
6
use Foolz\SphinxQL\Expression;
7
use mysqli;
8
use PDO;
9
10
abstract class ConnectionBase implements ConnectionInterface
11
{
12
    /**
13
     * The connection parameters for the database server.
14
     *
15
     * @var array
16
     */
17
    protected $connection_params = array('host' => '127.0.0.1', 'port' => 9306, 'socket' => null);
18
19
    /**
20
     * Internal connection object.
21
     * @var mysqli|PDO
22
     */
23
    protected $connection;
24
25
    /**
26
     * Sets one or more connection parameters.
27
     *
28
     * @param array $params Associative array of parameters and values.
29
     */
30
    public function setParams(array $params)
31
    {
32
        foreach ($params as $param => $value) {
33
            $this->setParam($param, $value);
34
        }
35
    }
36
37
    /**
38
     * Set a single connection parameter. Valid parameters include:
39
     *
40
     * * string host - The hostname, IP address, or unix socket
41
     * * int port - The port to the host
42
     * * array options - MySQLi options/values, as an associative array. Example: array(MYSQLI_OPT_CONNECT_TIMEOUT => 2)
43
     *
44
     * @param string $param Name of the parameter to modify.
45
     * @param mixed  $value Value to which the parameter will be set.
46
     */
47
    public function setParam($param, $value)
48
    {
49
        if ($param === 'host') {
50
            if ($value === 'localhost') {
51
                $value = '127.0.0.1';
52
            } elseif (stripos($value, 'unix:') === 0) {
53
                $param = 'socket';
54
            }
55
        }
56
        if ($param === 'socket') {
57
            if (stripos($value, 'unix:') === 0) {
58
                $value = substr($value, 5);
59
            }
60
            $this->connection_params['host'] = null;
61
        }
62
63
        $this->connection_params[$param] = $value;
64
    }
65
66
    /**
67
     * Returns the connection parameters (host, port, connection timeout) for the current instance.
68
     *
69
     * @return array $params The current connection parameters
70
     */
71
    public function getParams()
72
    {
73
        return $this->connection_params;
74
    }
75
76
    /**
77
     * Returns the current connection established.
78
     *
79
     * @return mysqli|PDO Internal connection object
80
     * @throws ConnectionException If no connection has been established or open
81
     */
82
    public function getConnection()
83
    {
84
        if (!is_null($this->connection)) {
85
            return $this->connection;
86
        }
87
88
        throw new ConnectionException('The connection to the server has not been established yet.');
89
    }
90
91
    /**
92
     * Adds quotes around values when necessary.
93
     * Based on FuelPHP's quoting function.
94
     * @inheritdoc
95
     */
96
    public function quote($value)
97
    {
98
        if ($value === null) {
99
            return 'null';
100
        } elseif ($value === true) {
101
            return 1;
102
        } elseif ($value === false) {
103
            return 0;
104
        } elseif ($value instanceof Expression) {
105
            // Use the raw expression
106
            return $value->value();
107
        } elseif (is_int($value)) {
108
            return (int) $value;
109
        } elseif (is_float($value)) {
110
            // Convert to non-locale aware float to prevent possible commas
111
            return sprintf('%F', $value);
112
        } elseif (is_array($value)) {
113
            // Supports MVA attributes
114
            return '('.implode(',', $this->quoteArr($value)).')';
115
        }
116
117
        return $this->escape($value);
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    public function quoteArr(array $array = array())
124
    {
125
        $result = array();
126
127
        foreach ($array as $key => $item) {
128
            $result[$key] = $this->quote($item);
129
        }
130
131
        return $result;
132
    }
133
134
    /**
135
     * Closes and unset the connection to the Sphinx server.
136
     *
137
     * @return $this
138
     * @throws ConnectionException
139
     */
140
    public function close()
141
    {
142
        $this->connection = null;
143
144
        return $this;
145
    }
146
147
    /**
148
     * Establishes a connection if needed
149
     * @throws ConnectionException
150
     */
151
    protected function ensureConnection()
152
    {
153
        try {
154
            $this->getConnection();
155
        } catch (ConnectionException $e) {
156
            $this->connect();
157
        }
158
    }
159
160
    /**
161
     * Establishes a connection to the Sphinx server.
162
     *
163
     * @return bool True if connected
164
     * @throws ConnectionException If a connection error was encountered
165
     */
166
    abstract public function connect();
167
168
}
169