Completed
Push — orm-dm-info ( abf332 )
by Nikolaos
02:10
created

Bind::bindInline()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.4661

Importance

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