| 1 | <?php |
||
| 14 | trait ResolvableObjectTrait { |
||
| 15 | |||
| 16 | 26 | public function resolve($value, array $args, ResolveInfo $info) |
|
| 17 | { |
||
| 18 | 26 | if ($this->resolveFunctionCache === null) { |
|
| 19 | 26 | $this->resolveFunctionCache = $this->getConfig()->getResolveFunction(); |
|
| 20 | |||
| 21 | 26 | if (!$this->resolveFunctionCache) { |
|
| 22 | 21 | $this->resolveFunctionCache = false; |
|
| 23 | } |
||
| 24 | } |
||
| 25 | 26 | if ($this->resolveFunctionCache) { |
|
| 26 | 25 | $resolveFunction = $this->resolveFunctionCache; |
|
| 27 | |||
| 28 | 25 | return $resolveFunction($value, $args, $info); |
|
| 29 | } else { |
||
| 30 | 21 | if (is_array($value) && array_key_exists($this->getName(), $value)) { |
|
| 31 | 15 | return $value[$this->getName()]; |
|
| 32 | 10 | } elseif (is_object($value)) { |
|
| 33 | 7 | return TypeService::getPropertyValue($value, $this->getName()); |
|
| 34 | 3 | } elseif ($this->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
| 35 | 3 | return null; |
|
| 36 | } else { |
||
| 37 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $this->getName())); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | public function getResolveFunction() { |
||
| 46 | } |
||
| 47 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: