1
|
|
|
<?php |
2
|
|
|
namespace Arthem\GraphQLMapper\Mapping\Guess; |
3
|
|
|
|
4
|
|
|
use Arthem\GraphQLMapper\Mapping\Context\FieldContext; |
5
|
|
|
use Arthem\GraphQLMapper\Mapping\Type; |
6
|
|
|
use Arthem\GraphQLMapper\Utils\StringHelper; |
7
|
|
|
|
8
|
|
|
class PropertyGuesser implements FieldGuesserInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* {@inheritdoc} |
12
|
|
|
*/ |
13
|
|
|
public function guessFieldResolveConfig(FieldContext $fieldContext) |
14
|
|
|
{ |
15
|
|
|
/** @var Type $type */ |
16
|
|
|
$type = $fieldContext->getContainer(); |
17
|
|
|
|
18
|
|
|
if (!empty($type->getModel())) { |
19
|
|
|
$className = $type->getModel(); |
20
|
|
|
if (null !== $accessor = $this->getAccessor($className, $fieldContext)) { |
21
|
|
|
return new ResolveConfigGuess([ |
22
|
|
|
'method' => $accessor, |
23
|
|
|
'handler' => 'property', |
24
|
|
|
], Guess::HIGH_CONFIDENCE); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $className |
31
|
|
|
* @param FieldContext $field |
|
|
|
|
32
|
|
|
* @return string|null |
33
|
|
|
*/ |
34
|
|
|
private function getAccessor($className, FieldContext $fieldContext) |
35
|
|
|
{ |
36
|
|
|
$field = $fieldContext->getField(); |
37
|
|
|
|
38
|
|
|
$class = new \ReflectionClass($className); |
39
|
|
|
|
40
|
|
|
$property = $field->getProperty() ?: $field->getName(); |
41
|
|
|
$camelName = StringHelper::camelize($property); |
42
|
|
|
|
43
|
|
|
$getter = 'get' . $camelName; |
44
|
|
|
$getsetter = lcfirst($camelName); |
45
|
|
|
$isser = 'is' . $camelName; |
46
|
|
|
$hasser = 'has' . $camelName; |
47
|
|
|
$test = [$getter, $getsetter, $isser, $hasser]; |
48
|
|
|
|
49
|
|
|
$reflMethods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); |
50
|
|
|
$methods = []; |
51
|
|
|
foreach ($reflMethods as $reflMethod) { |
52
|
|
|
$methods[$reflMethod->getName()] = true; |
53
|
|
|
} |
54
|
|
|
foreach ($test as $method) { |
55
|
|
|
if (isset($methods[$method])) { |
56
|
|
|
return $method; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function guessFieldType(FieldContext $fieldContext) |
65
|
|
|
{ |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.