Skip to content

Commit 98e785f

Browse files
authored
Merge branch 'master' into multiple-targetType
2 parents 689840b + 88c050e commit 98e785f

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

UPGRADE-0.13.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The new `default_field_resolver` config entry accepts callable service id.
2323
Stop using internally `symfony/property-access` package
2424
since it was a bottleneck to performance for large schema.
2525

26-
Array access and camelize getter are supported but isser, hasser,
26+
Array access and camelize getter/isser are supported but hasser,
2727
jQuery style (e.g. `last()`) and "can" property accessors
2828
are no more supported out-of-the-box,
2929
please implement a custom resolver if these accessors are needed.

src/Config/Parser/AnnotationParser.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ private static function getTypeFieldConfigurationFromReflector(GraphClass $graph
511511
if (!empty($fieldAnnotation->args)) {
512512
foreach ($fieldAnnotation->args as $arg) {
513513
$args[$arg->name] = ['type' => $arg->type]
514-
+ ($arg->description ? ['description' => $arg->description] : [])
515-
+ ($arg->default ? ['defaultValue' => $arg->default] : []);
514+
+ (null !== $arg->description ? ['description' => $arg->description] : [])
515+
+ (null !== $arg->default ? ['defaultValue' => $arg->default] : []);
516516
}
517517
} elseif ($reflector instanceof ReflectionMethod) {
518518
$args = self::guessArgs($reflector);
@@ -750,6 +750,28 @@ private static function getDescriptionConfiguration(array $annotations, bool $wi
750750
return $config;
751751
}
752752

753+
/**
754+
* Get args config from an array of @Arg annotation or by auto-guessing if a method is provided.
755+
*
756+
* @param array $args
757+
* @param ReflectionMethod $method
758+
*/
759+
private static function getArgs(array $args = null, ReflectionMethod $method = null): array
760+
{
761+
$config = [];
762+
if ($args && !empty($args)) {
763+
foreach ($args as $arg) {
764+
$config[$arg->name] = ['type' => $arg->type]
765+
+ ($arg->description ? ['description' => $arg->description] : [])
766+
+ ($arg->default ? ['defaultValue' => $arg->default] : []);
767+
}
768+
} elseif ($method) {
769+
$config = self::guessArgs($method);
770+
}
771+
772+
return $config;
773+
}
774+
753775
/**
754776
* Format an array of args to a list of arguments in an expression.
755777
*/

src/Resolver/FieldResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public static function valueFromObjectOrArray($objectOrArray, string $fieldName)
4242
} elseif (is_object($objectOrArray)) {
4343
if (null !== $getter = self::guessObjectMethod($objectOrArray, $fieldName, 'get')) {
4444
$value = $objectOrArray->$getter();
45+
} elseif (null !== $getter = self::guessObjectMethod($objectOrArray, $fieldName, 'is')) {
46+
$value = $objectOrArray->$getter();
4547
} elseif (null !== $getter = self::guessObjectMethod($objectOrArray, $fieldName, '')) {
4648
$value = $objectOrArray->$getter();
4749
} elseif (isset($objectOrArray->$fieldName)) {

tests/Config/Parser/AnnotationParserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function testTypes(): void
135135
'currentMaster' => ['type' => 'Sith', 'resolve' => "@=service('master_resolver').getMaster(value)"],
136136
'victims' => [
137137
'type' => '[Character]',
138-
'args' => ['jediOnly' => ['type' => 'Boolean', 'description' => 'Only Jedi victims']],
138+
'args' => ['jediOnly' => ['type' => 'Boolean', 'description' => 'Only Jedi victims', 'defaultValue' => false]],
139139
'resolve' => '@=call(value.getVictims, arguments({jediOnly: "Boolean"}, args))',
140140
],
141141
],

tests/Resolver/ResolverFieldTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ class ResolverFieldTest extends TestCase
1313
/**
1414
* @dataProvider resolverProvider
1515
*
16-
* @param mixed $source
16+
* @param mixed $source
17+
* @param bool|string|null $expected
1718
*/
18-
public function testDefaultFieldResolveFn(string $fieldName, $source, ?string $expected): void
19+
public function testDefaultFieldResolveFn(string $fieldName, $source, $expected): void
1920
{
2021
$info = $this->getMockBuilder(ResolveInfo::class)->disableOriginalConstructor()->getMock();
2122
$info->fieldName = $fieldName;
@@ -36,6 +37,8 @@ public function resolverProvider(): array
3637
['private_property_with_getter2', $object, Toto::PRIVATE_PROPERTY_WITH_GETTER2_VALUE],
3738
['not_object_or_array', 'String', null],
3839
['name', $object, $object->name],
40+
['enabled', $object, $object->isEnabled()],
41+
['isDisabled', $object, $object->isDisabled()],
3942
];
4043
}
4144
}

tests/Resolver/Toto.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class Toto
1717
private string $privatePropertyWithGetter = self::PRIVATE_PROPERTY_WITH_GETTER_VALUE;
1818
private string $private_property_with_getter2 = self::PRIVATE_PROPERTY_WITH_GETTER2_VALUE;
1919
public string $name = 'public';
20+
private bool $enabled = true;
21+
private bool $isDisabled = false;
2022

2123
public function getPrivatePropertyWithGetter(): string
2224
{
@@ -39,4 +41,14 @@ public function resolve(): array
3941
{
4042
return func_get_args();
4143
}
44+
45+
public function isEnabled(): bool
46+
{
47+
return $this->enabled;
48+
}
49+
50+
public function isDisabled(): bool
51+
{
52+
return $this->isDisabled;
53+
}
4254
}

0 commit comments

Comments
 (0)