Passed
Push — master ( c94b6d...d25cd5 )
by Nikolaos
02:58
created

Bind   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 137
ccs 42
cts 60
cp 0.7
rs 10
c 0
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A bindInline() 0 17 4
A setValue() 0 8 2
A setValues() 0 4 2
A getType() 0 15 4
A inlineArray() 0 14 2
A toArray() 0 3 1
A remove() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 *
11
 * Implementation of this file has been influenced by AtlasPHP
12
 *
13
 * @link    https://github.com/atlasphp/Atlas.Query
14
 * @license https://github.com/atlasphp/Atlas.Query/blob/1.x/LICENSE.md
15
 */
16
17
declare(strict_types=1);
18
19
namespace Phalcon\DataMapper\Query;
20
21
use PDO;
22
23
use function is_bool;
24
use function is_int;
25
26
/**
27
 * Class Bind
28
 *
29
 * @property int   $inlineCount
30
 * @property array $store
31
 */
32
class Bind
33
{
34
    /**
35
     * @var int
36
     */
37
    protected $inlineCount = 0;
38
39
    /**
40
     * @var array
41
     */
42
    protected $store = [];
43
44
    /**
45
     * @param mixed $value
46
     * @param int   $type
47
     *
48
     * @return string
49
     */
50 8
    public function bindInline($value, int $type = -1): string
51
    {
52 8
        if (is_object($value) && $value instanceof Select) {
53 1
            return "(" . $value->getStatement() . ")";
54
        }
55
56 8
        if (is_array($value)) {
57 3
            return $this->inlineArray($value, $type);
58
        }
59
60 8
        $this->inlineCount = $this->inlineCount + 1;
61
62 8
        $key = "__" . $this->inlineCount . "__";
63
64 8
        $this->setValue($key, $value, $type);
65
66 8
        return ":" . $key;
67
    }
68
69
    /**
70
     * Removes a value from the store
71
     *
72
     * @param string $key
73
     */
74 5
    public function remove(string $key): void
75
    {
76 5
        $store = $this->store;
77
78 5
        unset($store[$key]);
79
80 5
        $this->store = $store;
81 5
    }
82
83
    /**
84
     * Sets a value
85
     *
86
     * @param string $key
87
     * @param mixed  $value
88
     * @param int    $type
89
     */
90 19
    public function setValue(string $key, $value, int $type = -1): void
91
    {
92 19
        $localType = $type;
93 19
        if ($localType === -1) {
94 19
            $localType = $this->getType($value);
95
        }
96
97 19
        $this->store[$key] = [$value, $localType];
98 19
    }
99
100
    /**
101
     * Sets values from an array
102
     *
103
     * @param array $values
104
     * @param int   $type
105
     */
106 7
    public function setValues(array $values, int $type = -1): void
107
    {
108 7
        foreach ($values as $key => $value) {
109 7
            $this->setValue($key, $value, $type);
110
        }
111 7
    }
112
113
    /**
114
     * Returns the internal collection
115
     *
116
     * @return array
117
     */
118 17
    public function toArray(): array
119
    {
120 17
        return $this->store;
121
    }
122
123
    /**
124
     * Auto detects the PDO type
125
     *
126
     * @param mixed $value
127
     *
128
     * @return int
129
     */
130 19
    protected function getType($value): int
131
    {
132 19
        if (null === $value) {
133 4
            return PDO::PARAM_NULL;
134
        }
135
136 19
        if (is_bool($value)) {
137 4
            return PDO::PARAM_BOOL;
138
        }
139
140 19
        if (is_int($value)) {
141 17
            return PDO::PARAM_INT;
142
        }
143
144 14
        return PDO::PARAM_STR;
145
    }
146
147
    /**
148
     * Processes an array - if passed as an `inline` parameter
149
     *
150
     * @param array $data
151
     * @param int   $type
152
     *
153
     * @return string
154
     */
155 3
    protected function inlineArray(array $data, int $type): string
156
    {
157 3
        $keys = [];
158 3
        foreach ($data as $value) {
159 3
            $this->inlineCount = $this->inlineCount + 1;
160
161 3
            $key = "__" . $this->inlineCount . "__";
162
163 3
            $this->setValue($key, $value, $type);
164
165 3
            $keys[] = ":" . $key;
166
        }
167
168 3
        return "(" . implode(", ", $keys) . ")";
169
    }
170
}
171