1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php
namespace GraphQL\Utils;
use GraphQL\Error\Error;
use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Language\AST\Node;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\WrappingType;
use \Traversable, \InvalidArgumentException;
class Utils
{
public static function undefined()
{
static $undefined;
return $undefined ?: $undefined = new \stdClass();
}
/**
* Check if the value is invalid
*
* @param mixed $value
* @return bool
*/
public static function isInvalid($value)
{
return self::undefined() === $value;
}
/**
* @param object $obj
* @param array $vars
* @param array $requiredKeys
*
* @return object
*/
public static function assign($obj, array $vars, array $requiredKeys = [])
{
foreach ($requiredKeys as $key) {
if (!isset($vars[$key])) {
throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null");
}
}
foreach ($vars as $key => $value) {
if (!property_exists($obj, $key)) {
$cls = get_class($obj);
Warning::warn(
"Trying to set non-existing property '$key' on class '$cls'",
Warning::WARNING_ASSIGN
);
}
$obj->{$key} = $value;
}
return $obj;
}
/**
* @param array|Traversable $traversable
* @param callable $predicate
* @return null
*/
public static function find($traversable, callable $predicate)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
foreach ($traversable as $key => $value) {
if ($predicate($value, $key)) {
return $value;
}
}
return null;
}
/**
* @param $traversable
* @param callable $predicate
* @return array
* @throws \Exception
*/
public static function filter($traversable, callable $predicate)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$result = [];
$assoc = false;
foreach ($traversable as $key => $value) {
if (!$assoc && !is_int($key)) {
$assoc = true;
}
if ($predicate($value, $key)) {
$result[$key] = $value;
}
}
return $assoc ? $result : array_values($result);
}
/**
* @param array|\Traversable $traversable
* @param callable $fn function($value, $key) => $newValue
* @return array
* @throws \Exception
*/
public static function map($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
$map[$key] = $fn($value, $key);
}
return $map;
}
/**
* @param $traversable
* @param callable $fn
* @return array
* @throws \Exception
*/
public static function mapKeyValue($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
list($newKey, $newValue) = $fn($value, $key);
$map[$newKey] = $newValue;
}
return $map;
}
/**
* @param $traversable
* @param callable $keyFn function($value, $key) => $newKey
* @return array
* @throws \Exception
*/
public static function keyMap($traversable, callable $keyFn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$map = [];
foreach ($traversable as $key => $value) {
$newKey = $keyFn($value, $key);
if (is_scalar($newKey)) {
$map[$newKey] = $value;
}
}
return $map;
}
/**
* @param $traversable
* @param callable $fn
*/
public static function each($traversable, callable $fn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
foreach ($traversable as $key => $item) {
$fn($item, $key);
}
}
/**
* Splits original traversable to several arrays with keys equal to $keyFn return
*
* E.g. Utils::groupBy([1, 2, 3, 4, 5], function($value) {return $value % 3}) will output:
* [
* 1 => [1, 4],
* 2 => [2, 5],
* 0 => [3],
* ]
*
* $keyFn is also allowed to return array of keys. Then value will be added to all arrays with given keys
*
* @param $traversable
* @param callable $keyFn function($value, $key) => $newKey(s)
* @return array
*/
public static function groupBy($traversable, callable $keyFn)
{
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
$grouped = [];
foreach ($traversable as $key => $value) {
$newKeys = (array) $keyFn($value, $key);
foreach ($newKeys as $key) {
$grouped[$key][] = $value;
}
}
return $grouped;
}
/**
* @param array|Traversable $traversable
* @param callable $keyFn
* @param callable $valFn
* @return array
*/
public static function keyValMap($traversable, callable $keyFn, callable $valFn)
{
$map = [];
foreach ($traversable as $item) {
$map[$keyFn($item)] = $valFn($item);
}
return $map;
}
/**
* @param $traversable
* @param callable $predicate
* @return bool
*/
public static function every($traversable, callable $predicate)
{
foreach ($traversable as $key => $value) {
if (!$predicate($value, $key)) {
return false;
}
}
return true;
}
/**
* @param $test
* @param string $message
* @param mixed $sprintfParam1
* @param mixed $sprintfParam2 ...
* @throws Error
*/
public static function invariant($test, $message = '')
{
if (!$test) {
if (func_num_args() > 2) {
$args = func_get_args();
array_shift($args);
$message = call_user_func_array('sprintf', $args);
}
// TODO switch to Error here
throw new InvariantViolation($message);
}
}
/**
* @param $var
* @return string
*/
public static function getVariableType($var)
{
if ($var instanceof Type) {
// FIXME: Replace with schema printer call
if ($var instanceof WrappingType) {
$var = $var->getWrappedType(true);
}
return $var->name;
}
return is_object($var) ? get_class($var) : gettype($var);
}
/**
* @param mixed $var
* @return string
*/
public static function printSafeJson($var)
{
if ($var instanceof \stdClass) {
$var = (array) $var;
}
if (is_array($var)) {
return json_encode($var);
}
if ('' === $var) {
return '(empty string)';
}
if (null === $var) {
return 'null';
}
if (false === $var) {
return 'false';
}
if (true === $var) {
return 'true';
}
if (is_string($var)) {
return "\"$var\"";
}
if (is_scalar($var)) {
return (string) $var;
}
return gettype($var);
}
/**
* @param $var
* @return string
*/
public static function printSafe($var)
{
if ($var instanceof Type) {
return $var->toString();
}
if (is_object($var)) {
if (method_exists($var, '__toString')) {
return (string) $var;
} else {
return 'instance of ' . get_class($var);
}
}
if (is_array($var)) {
return json_encode($var);
}
if ('' === $var) {
return '(empty string)';
}
if (null === $var) {
return 'null';
}
if (false === $var) {
return 'false';
}
if (true === $var) {
return 'true';
}
if (is_string($var)) {
return $var;
}
if (is_scalar($var)) {
return (string) $var;
}
return gettype($var);
}
/**
* UTF-8 compatible chr()
*
* @param string $ord
* @param string $encoding
* @return string
*/
public static function chr($ord, $encoding = 'UTF-8')
{
if ($ord <= 255) {
return chr($ord);
}
if ($encoding === 'UCS-4BE') {
return pack("N", $ord);
} else {
return mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
}
}
/**
* UTF-8 compatible ord()
*
* @param string $char
* @param string $encoding
* @return mixed
*/
public static function ord($char, $encoding = 'UTF-8')
{
if (!$char && '0' !== $char) {
return 0;
}
if (!isset($char[1])) {
return ord($char);
}
if ($encoding !== 'UCS-4BE') {
$char = mb_convert_encoding($char, 'UCS-4BE', $encoding);
}
list(, $ord) = unpack('N', $char);
return $ord;
}
/**
* Returns UTF-8 char code at given $positing of the $string
*
* @param $string
* @param $position
* @return mixed
*/
public static function charCodeAt($string, $position)
{
$char = mb_substr($string, $position, 1, 'UTF-8');
return self::ord($char);
}
/**
* @param $code
* @return string
*/
public static function printCharCode($code)
{
if (null === $code) {
return '<EOF>';
}
return $code < 0x007F
// Trust JSON for ASCII.
? json_encode(Utils::chr($code))
// Otherwise print the escaped form.
: '"\\u' . dechex($code) . '"';
}
/**
* Upholds the spec rules about naming.
*
* @param $name
* @throws Error
*/
public static function assertValidName($name)
{
$error = self::isValidNameError($name);
if ($error) {
throw $error;
}
}
/**
* Returns an Error if a name is invalid.
*
* @param string $name
* @param Node|null $node
* @return Error|null
*/
public static function isValidNameError($name, $node = null)
{
Utils::invariant(is_string($name), 'Expected string');
if (isset($name[1]) && $name[0] === '_' && $name[1] === '_') {
return new Error(
"Name \"{$name}\" must not begin with \"__\", which is reserved by " .
"GraphQL introspection.",
$node
);
}
if (!preg_match('/^[_a-zA-Z][_a-zA-Z0-9]*$/', $name)) {
return new Error(
"Names must match /^[_a-zA-Z][_a-zA-Z0-9]*\$/ but \"{$name}\" does not.",
$node
);
}
return null;
}
/**
* Wraps original closure with PHP error handling (using set_error_handler).
* Resulting closure will collect all PHP errors that occur during the call in $errors array.
*
* @param callable $fn
* @param \ErrorException[] $errors
* @return \Closure
*/
public static function withErrorHandling(callable $fn, array &$errors)
{
return function() use ($fn, &$errors) {
// Catch custom errors (to report them in query results)
set_error_handler(function ($severity, $message, $file, $line) use (&$errors) {
$errors[] = new \ErrorException($message, 0, $severity, $file, $line);
});
try {
return $fn();
} finally {
restore_error_handler();
}
};
}
/**
* @param string[] $items
* @return string
*/
public static function quotedOrList(array $items)
{
$items = array_map(function($item) { return "\"$item\""; }, $items);
return self::orList($items);
}
public static function orList(array $items)
{
if (!$items) {
throw new \LogicException('items must not need to be empty.');
}
$selected = array_slice($items, 0, 5);
$selectedLength = count($selected);
$firstSelected = $selected[0];
if ($selectedLength === 1) {
return $firstSelected;
}
return array_reduce(
range(1, $selectedLength - 1),
function ($list, $index) use ($selected, $selectedLength) {
return $list.
($selectedLength > 2 ? ', ' : ' ') .
($index === $selectedLength - 1 ? 'or ' : '') .
$selected[$index];
},
$firstSelected
);
}
/**
* Given an invalid input string and a list of valid options, returns a filtered
* list of valid options sorted based on their similarity with the input.
*
* Includes a custom alteration from Damerau-Levenshtein to treat case changes
* as a single edit which helps identify mis-cased values with an edit distance
* of 1
* @param string $input
* @param array $options
* @return string[]
*/
public static function suggestionList($input, array $options)
{
$optionsByDistance = [];
$inputThreshold = mb_strlen($input) / 2;
foreach ($options as $option) {
$distance = $input === $option
? 0
: (strtolower($input) === strtolower($option)
? 1
: levenshtein($input, $option));
$threshold = max($inputThreshold, mb_strlen($option) / 2, 1);
if ($distance <= $threshold) {
$optionsByDistance[$option] = $distance;
}
}
asort($optionsByDistance);
return array_keys($optionsByDistance);
}
}