GraphQL\Utils::find PHP Method

find() public static method

public static find ( array | Traversabl\Traversable $traversable, callable $predicate ) : null
$traversable array | Traversabl\Traversable
$predicate callable
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;
    }

Usage Example

Example #1
0
 /**
  * Determines if a field should be included based on the @include and @skip
  * directives, where @skip has higher precedence than @include.
  *
  * @param ExecutionContext $exeContext
  * @param $directives
  * @return bool
  */
 private static function shouldIncludeNode(ExecutionContext $exeContext, $directives)
 {
     $skipDirective = Directive::skipDirective();
     $includeDirective = Directive::includeDirective();
     /** @var \GraphQL\Language\AST\DirectiveNode $skipNode */
     $skipNode = $directives ? Utils::find($directives, function (\GraphQL\Language\AST\DirectiveNode $directive) use($skipDirective) {
         return $directive->name->value === $skipDirective->name;
     }) : null;
     if ($skipNode) {
         $argValues = Values::getArgumentValues($skipDirective, $skipNode, $exeContext->variableValues);
         if (isset($argValues['if']) && $argValues['if'] === true) {
             return false;
         }
     }
     /** @var \GraphQL\Language\AST\DirectiveNode $includeNode */
     $includeNode = $directives ? Utils::find($directives, function (\GraphQL\Language\AST\DirectiveNode $directive) use($includeDirective) {
         return $directive->name->value === $includeDirective->name;
     }) : null;
     if ($includeNode) {
         $argValues = Values::getArgumentValues($includeDirective, $includeNode, $exeContext->variableValues);
         if (isset($argValues['if']) && $argValues['if'] === false) {
             return false;
         }
     }
     return true;
 }
All Usage Examples Of GraphQL\Utils::find