Completed
Pull Request — master (#106)
by Christoffer
02:56
created

orList()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 2
nop 1
1
<?php
2
3
namespace Digia\GraphQL\Util;
4
5
const MAX_LENGTH = 5;
6
7
/**
8
 * Given `[A, B, C]` returns `'A, B or C'`.
9
 *
10
 * @param array $items
11
 * @return string
12
 */
13
function orList(array $items): string
14
{
15
    $selected = \array_slice($items, 0, MAX_LENGTH);
16
    $count    = \count($selected);
17
    $index    = 0;
18
19
    return $count === 1
20
        ? $selected[0]
21
        : array_reduce($selected, function ($list, $item) use ($count, &$index) {
22
            $list .= ($index > 0 && $index < ($count - 1) ? ', ' : '') . ($index === ($count - 1) ? ' or ' : '') .
23
                $item;
24
            $index++;
25
            return $list;
26
        }, '');
27
}
28
29
/**
30
 * Given `[A, B, C]` returns `'"A", "B" or "C"'`.
31
 *
32
 * @param array $items
33
 * @return string
34
 */
35
function quotedOrList(array $items): string
36
{
37
    return orList(array_map(function ($item) {
38
        return '"' . $item . '"';
39
    }, $items));
40
}
41