Skip to content

Commit 54e1cf2

Browse files
author
Dustin Breuer
committed
Adds isTypeOf to TypeBuilder
1 parent a77a314 commit 54e1cf2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Generator/TypeBuilder.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ protected function buildConfig(array $config): Collection
329329
$configLoader->addItem('resolveType', $this->buildResolveType($c->resolveType));
330330
}
331331

332+
if (isset($c->isTypeOf)) {
333+
$configLoader->addItem('isTypeOf', $this->buildIsTypeOf($c->isTypeOf));
334+
}
335+
332336
if (isset($c->resolveField)) {
333337
$configLoader->addItem('resolveField', $this->buildResolve($c->resolveField));
334338
}
@@ -923,6 +927,32 @@ protected function buildResolveType($resolveType)
923927
return $resolveType;
924928
}
925929

930+
/**
931+
* Builds an arrow function from a string with an expression prefix,
932+
* otherwise just returns the provided value back untouched.
933+
*
934+
* Render example:
935+
*
936+
* fn($className) => (($className = "App\\ClassName") && $value instanceof $className)
937+
*
938+
* @param mixed $isTypeOf
939+
*
940+
* @return mixed|ArrowFunction
941+
*/
942+
private function buildIsTypeOf($isTypeOf)
943+
{
944+
if (EL::isStringWithTrigger($isTypeOf)) {
945+
$expression = $this->expressionConverter->convert($isTypeOf);
946+
947+
return ArrowFunction::new()
948+
->addArguments('className')
949+
->setExpression(Literal::new($expression))
950+
->setStatic();
951+
}
952+
953+
return $isTypeOf;
954+
}
955+
926956
/**
927957
* Creates and array from a formatted string.
928958
*

0 commit comments

Comments
 (0)