Completed
Push — 6.0 ( be0b6e...c47dd5 )
by liu
06:54 queued 10s
created

ParamsBind::getBind()   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 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\db\concern;
14
15
use PDO;
16
17
/**
18
 * 参数绑定支持
19
 */
20
trait ParamsBind
21
{
22
    /**
23
     * 当前参数绑定
24
     * @var array
25
     */
26
    protected $bind = [];
27
28
    /**
29
     * 批量参数绑定
30
     * @access public
31
     * @param array $value 绑定变量值
32
     * @return $this
33
     */
34
    public function bind(array $value)
35
    {
36
        $this->bind = array_merge($this->bind, $value);
37
        return $this;
38
    }
39
40
    /**
41
     * 单个参数绑定
42
     * @access public
43
     * @param mixed   $value 绑定变量值
44
     * @param integer $type  绑定类型
45
     * @param string  $name  绑定标识
46
     * @return string
47
     */
48
    public function bindValue($value, int $type = null, string $name = null)
49
    {
50
        $name = $name ?: 'ThinkBind_' . (count($this->bind) + 1) . '_';
51
52
        $this->bind[$name] = [$value, $type ?: PDO::PARAM_STR];
53
        return $name;
54
    }
55
56
    /**
57
     * 检测参数是否已经绑定
58
     * @access public
59
     * @param string $key 参数名
60
     * @return bool
61
     */
62
    public function isBind($key)
63
    {
64
        return isset($this->bind[$key]);
65
    }
66
67
    /**
68
     * 参数绑定
69
     * @access public
70
     * @param string $sql  绑定的sql表达式
71
     * @param array  $bind 参数绑定
72
     * @return void
73
     */
74
    protected function bindParams(string &$sql, array $bind = []): void
75
    {
76
        foreach ($bind as $key => $value) {
77
            if (is_array($value)) {
78
                $name = $this->bindValue($value[0], $value[1], $value[2] ?? null);
79
            } else {
80
                $name = $this->bindValue($value);
81
            }
82
83
            if (is_numeric($key)) {
84
                $sql = substr_replace($sql, ':' . $name, strpos($sql, '?'), 1);
85
            } else {
86
                $sql = str_replace(':' . $key, ':' . $name, $sql);
87
            }
88
        }
89
    }
90
91
    /**
92
     * 获取绑定的参数 并清空
93
     * @access public
94
     * @param bool $clear 是否清空绑定数据
95
     * @return array
96
     */
97
    public function getBind(bool $clear = true): array
98
    {
99
        $bind = $this->bind;
100
        if ($clear) {
101
            $this->bind = [];
102
        }
103
104
        return $bind;
105
    }
106
}
107