Completed
Push — master ( b4b898...f81b53 )
by Woody
11s
created

functions.php ➔ alias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Latitude\QueryBuilder;
5
6
function isStatement($value): bool
7
{
8 57
    return $value instanceof StatementInterface;
9
}
10
11
function alias($field, string $alias): ExpressionInterface
12
{
13 10
    return express('%s AS %s', identify($field), identify($alias));
14
}
15
16
function fn(string $function, string ...$fields): ExpressionInterface
17
{
18 3
    return express("$function(%s)", listing(identifyAll($fields)));
19
}
20
21
function literal($value): StatementInterface
22
{
23 5
    return isStatement($value) ? $value : new Partial\Literal($value);
24
}
25
26
function on(string $left, string $right): CriteriaInterface
27
{
28 6
    return criteria('%s = %s', identify($left), identify($right));
29
}
30
31
function order(string $column, string $direction = null): StatementInterface
32
{
33 4
    if (empty($direction)) {
34 3
        return identify($column);
35
    }
36 2
    return express(sprintf("%%s %s", strtoupper($direction)), identify($column));
37
}
38
39
function criteria(string $pattern, ...$replacements): CriteriaInterface
40
{
41 31
    return new Partial\Criteria(express($pattern, ...$replacements));
42
}
43
44
function group(CriteriaInterface $criteria): CriteriaInterface
45
{
46 1
    return criteria('(%s)', $criteria);
47
}
48
49
function field($name): Builder\CriteriaBuilder
50
{
51 16
    return new Builder\CriteriaBuilder(identify($name));
52
}
53
54
function search($name): Builder\LikeBuilder
55
{
56 4
    return new Builder\LikeBuilder(identify($name));
57
}
58
59
function express(string $pattern, ...$replacements): ExpressionInterface
60
{
61 53
    return new Partial\Expression($pattern, ...paramAll($replacements));
62
}
63
64
function identify($name): StatementInterface
65
{
66 57
    if (isStatement($name)) {
67 8
        return $name;
68
    }
69
70 57
    if (strpos($name, '.') === false) {
71 57
        return new Partial\Identifier($name);
72
    }
73
74 11
    return new Partial\IdentifierQualified(...identifyAll(explode('.', $name)));
75
}
76
77
/**
78
 * @return StatementInterface[]
79
 */
80
function identifyAll(array $names): array
81
{
82 20
    return array_map('Latitude\QueryBuilder\identify', $names);
83
}
84
85
function param($value): StatementInterface
86
{
87 44
    return isStatement($value) ? $value : new Partial\Parameter($value);
88
}
89
90
/**
91
 * @return StatementInterface[]
92
 */
93
function paramAll(array $values): array
94
{
95 53
    return array_map('Latitude\QueryBuilder\param', $values);
96
}
97
98
function listing(array $values, string $separator = ', '): Partial\Listing
99
{
100 24
    return new Partial\Listing($separator, ...paramAll($values));
101
}
102