elastics_encode()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.3731

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 15
ccs 5
cts 7
cp 0.7143
crap 4.3731
rs 9.2
c 1
b 0
f 0
1
<?php
2
/*
3
 * This file is part of PHPinnacle/Elastics.
4
 *
5
 * (c) PHPinnacle Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
use PHPinnacle\Elastics\Query;
14
15
/**
16
 * @param Query[] ...$queries
17
 *
18
 * @return array
19
 */
20
function elastics_compile(Query ...$queries): array
21
{
22
    $list = \array_map(function (Query $query) {
23
        return [
24 20
            $query->name() => $query->compile(),
25
        ];
26 20
    }, $queries);
27
28 20
    return \count($list) === 1 ? current($list) : $list;
29
}
30
31
/**
32
 * @param mixed $value
33
 * @param bool  $pretty
34
 *
35
 * @return string
36
 */
37
function elastics_encode($value, bool $pretty = false): string
38
{
39 1
    $flags = \JSON_BIGINT_AS_STRING | \JSON_UNESCAPED_UNICODE;
40
41 1
    if ($pretty) {
42
        $flags |= \JSON_PRETTY_PRINT;
43
    }
44
45 1
    $encoded = \json_encode($value, $flags);
46
47 1
    if ($encoded === false && $value !== false) {
48
        throw new \InvalidArgumentException(\json_last_error_msg());
49
    }
50
51 1
    return $encoded;
52
}
53