Passed
Push — master ( d25cd5...5cf75b )
by Nikolaos
03:08
created

Bind::setValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
    public function bindInline($value, int $type = -1): string
51
    {
52
        if (is_object($value) && $value instanceof Select) {
53
            return "(" . $value->getStatement() . ")";
54
        }
55
56
        if (is_array($value)) {
57
            return $this->inlineArray($value, $type);
58
        }
59
60
        $this->inlineCount = $this->inlineCount + 1;
61
62
        $key = "__" . $this->inlineCount . "__";
63
64
        $this->setValue($key, $value, $type);
65
66
        return ":" . $key;
67
    }
68
69
    /**
70
     * Removes a value from the store
71
     *
72
     * @param string $key
73
     */
74
    public function remove(string $key): void
75
    {
76
        $store = $this->store;
77
78
        unset($store[$key]);
79
80
        $this->store = $store;
81
    }
82
83
    /**
84
     * Sets a value
85
     *
86
     * @param string $key
87
     * @param mixed  $value
88
     * @param int    $type
89
     */
90
    public function setValue(string $key, $value, int $type = -1): void
91
    {
92
        $localType = $type;
93
        if ($localType === -1) {
94
            $localType = $this->getType($value);
95
        }
96
97
        $this->store[$key] = [$value, $localType];
98
    }
99
100
    /**
101
     * Sets values from an array
102
     *
103
     * @param array $values
104
     * @param int   $type
105
     */
106
    public function setValues(array $values, int $type = -1): void
107
    {
108
        foreach ($values as $key => $value) {
109
            $this->setValue($key, $value, $type);
110
        }
111
    }
112
113
    /**
114
     * Returns the internal collection
115
     *
116
     * @return array
117
     */
118
    public function toArray(): array
119
    {
120
        return $this->store;
121
    }
122
123
    /**
124
     * Auto detects the PDO type
125
     *
126
     * @param mixed $value
127
     *
128
     * @return int
129
     */
130
    protected function getType($value): int
131
    {
132
        if (null === $value) {
133
            return PDO::PARAM_NULL;
134
        }
135
136
        if (is_bool($value)) {
137
            return PDO::PARAM_BOOL;
138
        }
139
140
        if (is_int($value)) {
141
            return PDO::PARAM_INT;
142
        }
143
144
        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
    protected function inlineArray(array $data, int $type): string
156
    {
157
        $keys = [];
158
        foreach ($data as $value) {
159
            $this->inlineCount = $this->inlineCount + 1;
160
161
            $key = "__" . $this->inlineCount . "__";
162
163
            $this->setValue($key, $value, $type);
164
165
            $keys[] = ":" . $key;
166
        }
167
168
        return "(" . implode(", ", $keys) . ")";
169
    }
170
}
171