2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL;
|
|
|
|
|
2016-10-21 13:42:27 +03:00
|
|
|
use GraphQL\Error\InvariantViolation;
|
2016-05-02 00:42:05 +03:00
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Type\Definition\WrappingType;
|
|
|
|
use GraphQL\Utils\SchemaUtils;
|
2015-08-30 10:44:52 +03:00
|
|
|
use \Traversable, \InvalidArgumentException;
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
class Utils
|
|
|
|
{
|
|
|
|
/**
|
2015-08-30 10:44:52 +03:00
|
|
|
* @param object $obj
|
2015-07-15 20:05:46 +03:00
|
|
|
* @param array $vars
|
2015-08-30 10:44:52 +03:00
|
|
|
* @return array
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
|
|
|
public static function assign($obj, array $vars, array $requiredKeys = array())
|
|
|
|
{
|
|
|
|
foreach ($requiredKeys as $key) {
|
|
|
|
if (!isset($key, $vars)) {
|
2015-08-30 10:44:52 +03:00
|
|
|
throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null");
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($vars as $key => $value) {
|
|
|
|
if (!property_exists($obj, $key)) {
|
|
|
|
$cls = get_class($obj);
|
|
|
|
trigger_error("Trying to set non-existing property '$key' on class '$cls'");
|
|
|
|
}
|
|
|
|
$obj->{$key} = $value;
|
|
|
|
}
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-30 10:44:52 +03:00
|
|
|
* @param array|Traversable $traversable
|
|
|
|
* @param callable $predicate
|
2015-07-15 20:05:46 +03:00
|
|
|
* @return null
|
|
|
|
*/
|
2015-08-30 10:44:52 +03:00
|
|
|
public static function find($traversable, callable $predicate)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2015-08-30 10:44:52 +03:00
|
|
|
self::invariant(is_array($traversable) || $traversable instanceof \Traversable, __METHOD__ . ' expects array or Traversable');
|
|
|
|
|
|
|
|
foreach ($traversable as $key => $value) {
|
|
|
|
if ($predicate($value, $key)) {
|
|
|
|
return $value;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-09-06 12:50:16 +03:00
|
|
|
/**
|
|
|
|
* @param $traversable
|
|
|
|
* @param callable $predicate
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2015-08-30 10:44:52 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-17 14:34:51 +03:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2015-08-30 10:44:52 +03:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2015-10-17 14:34:51 +03:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 12:50:16 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2016-05-02 00:42:05 +03:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
/**
|
|
|
|
* @param $test
|
|
|
|
* @param string $message
|
|
|
|
* @param mixed $sprintfParam1
|
|
|
|
* @param mixed $sprintfParam2 ...
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
2016-10-21 13:42:27 +03:00
|
|
|
throw new InvariantViolation($message);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $var
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getVariableType($var)
|
|
|
|
{
|
2016-05-02 00:42:05 +03:00
|
|
|
if ($var instanceof Type) {
|
|
|
|
// FIXME: Replace with schema printer call
|
|
|
|
if ($var instanceof WrappingType) {
|
|
|
|
$var = $var->getWrappedType(true);
|
|
|
|
}
|
|
|
|
return $var->name;
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
return is_object($var) ? get_class($var) : gettype($var);
|
|
|
|
}
|
|
|
|
|
2016-10-18 21:34:46 +03:00
|
|
|
/**
|
|
|
|
* @param $var
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function printSafe($var)
|
|
|
|
{
|
|
|
|
if ($var instanceof Type) {
|
|
|
|
// FIXME: Replace with schema printer call
|
|
|
|
if ($var instanceof WrappingType) {
|
|
|
|
$var = $var->getWrappedType(true);
|
|
|
|
}
|
|
|
|
return $var->name;
|
|
|
|
}
|
|
|
|
if (is_object($var)) {
|
|
|
|
return 'instance of ' . get_class($var);
|
|
|
|
}
|
|
|
|
if (is_scalar($var)) {
|
|
|
|
return (string) $var;
|
|
|
|
}
|
|
|
|
if (null === $var) {
|
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
return gettype($var);
|
|
|
|
}
|
|
|
|
|
2015-08-30 10:44:52 +03:00
|
|
|
/**
|
|
|
|
* UTF-8 compatible chr()
|
|
|
|
*
|
|
|
|
* @param string $ord
|
|
|
|
* @param string $encoding
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
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()
|
|
|
|
*
|
2015-08-30 10:44:52 +03:00
|
|
|
* @param string $char
|
2015-07-15 20:05:46 +03:00
|
|
|
* @param string $encoding
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function ord($char, $encoding = 'UTF-8')
|
|
|
|
{
|
|
|
|
if (!isset($char[1])) {
|
|
|
|
return ord($char);
|
|
|
|
}
|
|
|
|
if ($encoding === 'UCS-4BE') {
|
2015-08-30 10:44:52 +03:00
|
|
|
list(, $ord) = (strlen($char) === 4) ? unpack('N', $char) : unpack('n', $char);
|
2015-07-15 20:05:46 +03:00
|
|
|
return $ord;
|
|
|
|
} else {
|
|
|
|
return self::ord(mb_convert_encoding($char, 'UCS-4BE', $encoding), 'UCS-4BE');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-30 10:44:52 +03:00
|
|
|
* Returns UTF-8 char code at given $positing of the $string
|
2015-07-15 20:05:46 +03:00
|
|
|
*
|
2015-08-30 10:44:52 +03:00
|
|
|
* @param $string
|
|
|
|
* @param $position
|
|
|
|
* @return mixed
|
2015-07-15 20:05:46 +03:00
|
|
|
*/
|
2015-08-30 10:44:52 +03:00
|
|
|
public static function charCodeAt($string, $position)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2015-08-30 10:44:52 +03:00
|
|
|
$char = mb_substr($string, $position, 1, 'UTF-8');
|
|
|
|
return self::ord($char);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2016-04-23 16:43:10 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) . '"';
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|