Completed
Pull Request — master (#19)
by Christoffer
02:15
created

keyMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL\Util;
4
5
use Digia\GraphQL\Error\GraphQLError;
6
7
/**
8
 * @param bool   $condition
9
 * @param string $message
10
 * @throws GraphQLError
11
 */
12
function invariant(bool $condition, string $message)
13
{
14
    if (!$condition) {
15
        throw new GraphQLError($message);
16
    }
17
}
18
19
/**
20
 * @param array    $array
21
 * @param callable $fn
22
 * @return mixed
23
 */
24
function arraySome(array $array, callable $fn)
25
{
26
    return array_reduce($array, function ($result, $value) use ($fn) {
27
        return $result || $fn($value);
28
    });
29
}
30
31
/**
32
 * @param array    $array
33
 * @param callable $keyFn
34
 * @return array
35
 */
36
function keyMap(array $array, callable $keyFn): array
37
{
38
    return array_reduce($array, function ($map, $item) use ($keyFn) {
39
        $map[$keyFn($item)] = $item;
40
        return $map;
41
    }, []);
42
}
43
44
/**
45
 * @param array    $array
46
 * @param callable $keyFn
47
 * @param callable $valFn
48
 * @return array
49
 */
50
function keyValMap(array $array, callable $keyFn, callable $valFn): array
51
{
52
    return array_reduce($array, function ($map, $item) use ($keyFn, $valFn) {
53
        $map[$keyFn($item)] = $valFn($item);
54
        return $map;
55
    }, []);
56
}
57
58
/**
59
 * @param $value
60
 * @return string
61
 */
62
function toString($value): string
63
{
64
    if (is_object($value) && method_exists($value, '__toString')) {
65
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type object which is incompatible with the type-hinted return string.
Loading history...
66
    }
67
    if (is_object($value)) {
68
        return 'Object';
69
    }
70
    if (is_array($value)) {
71
        return 'Array';
72
    }
73
    if (is_callable($value)) {
74
        return 'Function';
75
    }
76
    if ($value === '') {
77
        return '(empty string)';
78
    }
79
    if ($value === null) {
80
        return 'null';
81
    }
82
    if ($value === true) {
83
        return 'true';
84
    }
85
    if ($value === false) {
86
        return 'false';
87
    }
88
    if (is_string($value)) {
89
        return "\"{$value}\"";
90
    }
91
    if (is_scalar($value)) {
92
        return (string)$value;
93
    }
94
    return gettype($value);
95
}
96
97
/**
98
 * @param $value
99
 * @return string
100
 */
101
function jsonEncode($value): string
102
{
103
    return json_encode($value, JSON_UNESCAPED_UNICODE);
104
}
105
106
/**
107
 * @param string $path
108
 * @return string
109
 */
110
function readFile(string $path): string
111
{
112
    return mb_convert_encoding(file_get_contents($path), 'UTF-8');
113
}
114