functions.php ➔ random_string()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 38
rs 9.312
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\Where;
4
5
use BenTools\Where\DeleteQuery\DeleteQueryBuilder;
6
use BenTools\Where\Expression\Condition;
7
use BenTools\Where\Expression\Expression;
8
use BenTools\Where\Expression\GroupExpression;
9
use BenTools\Where\Expression\NegatedExpression;
10
use BenTools\Where\Helper\CaseHelper;
11
use BenTools\Where\Helper\FieldHelper;
12
use BenTools\Where\InsertQuery\InsertQueryBuilder;
13
use BenTools\Where\SelectQuery\SelectQueryBuilder;
14
use BenTools\Where\UpdateQuery\UpdateQueryBuilder;
15
16
/**
17
 * @param string|Expression $expression
18
 * @param array             ...$values
19
 * @return Expression|Condition
20
 * @throws \InvalidArgumentException
21
 */
22
function where($expression, ...$values): Expression
23
{
24
    return Expression::where($expression, ...$values);
25
}
26
27
/**
28
 * @param string|Expression $expression
29
 * @param array             ...$values
30
 * @return GroupExpression
31
 * @throws \InvalidArgumentException
32
 */
33
function group($expression, ...$values): GroupExpression
34
{
35
    return Expression::group($expression, ...$values);
36
}
37
38
/**
39
 * @param string|Expression $expression
40
 * @param array             ...$values
41
 * @return NegatedExpression
42
 * @throws \InvalidArgumentException
43
 */
44
function not($expression, ...$values): NegatedExpression
45
{
46
    return Expression::not($expression, ...$values);
47
}
48
49
function valuesOf(Expression ...$expressions): array
50
{
51
    return Expression::valuesOf(...$expressions);
52
}
53
54
/**
55
 * @param Expression[]|string[] ...$columns
56
 * @return SelectQueryBuilder
57
 */
58
function select(...$columns): SelectQueryBuilder
59
{
60
    return SelectQueryBuilder::make(...$columns);
61
}
62
63
/**
64
 * @param Expression[]|string[] ...$tables
65
 * @return SelectQueryBuilder
66
 */
67
function delete(...$tables): DeleteQueryBuilder
68
{
69
    return DeleteQueryBuilder::make(...$tables);
70
}
71
72
/**
73
 * @param array[] ...$values
74
 * @return InsertQueryBuilder
75
 * @throws \InvalidArgumentException
76
 */
77
function insert(array ...$values): InsertQueryBuilder
78
{
79
    return InsertQueryBuilder::load(...$values);
80
}
81
82
/**
83
 * @param string $table
84
 * @return UpdateQueryBuilder
85
 */
86
function update(string $table): UpdateQueryBuilder
87
{
88
    return UpdateQueryBuilder::make($table);
89
}
90
91
/**
92
 * @param array  $values
93
 * @param string $placeholder
94
 * @param string $glue
95
 * @return string
96
 * @internal
97
 */
98
function placeholders(array $values, string $placeholder = '?', string $glue = ', '): string
99
{
100
    return \implode($glue, \array_fill(0, \count($values), $placeholder));
101
}
102
103
/**
104
 * @param array  $values
105
 * @return array|false
106
 * @internal
107
 */
108
function random_placeholders(array $values)
109
{
110
    $placeholders = \array_map(
111
        function () {
112
            return random_string();
113
        },
114
        $values
115
    );
116
117
    return $placeholders;
118
}
119
120
/**
121
 * @param int $length
122
 * @return string
123
 * @throws \Exception
124
 * @internal
125
 */
126
function random_string(int $length = 8)
127
{
128
    $chars = [
129
        'a',
130
        'b',
131
        'c',
132
        'd',
133
        'e',
134
        'f',
135
        'g',
136
        'h',
137
        'i',
138
        'j',
139
        'k',
140
        'l',
141
        'm',
142
        'n',
143
        'o',
144
        'p',
145
        'q',
146
        'r',
147
        's',
148
        't',
149
        'u',
150
        'v',
151
        'w',
152
        'x',
153
        'y',
154
        'z',
155
    ];
156
157
    $string = '';
158
    for ($i = 0; $i < $length; $i++) {
159
        $string .= $chars[\random_int(0, 25)];
160
    }
161
162
    return $string;
163
}
164
165
/**
166
 * @param string $field
167
 * @return FieldHelper
168
 */
169
function field(string $field): FieldHelper
170
{
171
    return new FieldHelper($field);
172
}
173
174
/**
175
 * @param null  $expression
176
 * @param array ...$values
177
 * @return CaseHelper
178
 * @throws \InvalidArgumentException
179
 */
180
function conditionnal($expression = null, ...$values): CaseHelper
181
{
182
    return CaseHelper::create($expression, ...$values);
183
}
184
185
/**
186
 * @param null  $expression
187
 * @param array ...$values
188
 * @return CaseHelper
189
 */
190
function when($expression = null, ...$values): CaseHelper
191
{
192
    return CaseHelper::create()->when($expression, ...$values);
193
}
194