1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Resolver; |
13
|
|
|
|
14
|
|
|
abstract class AbstractResolver implements ResolverInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $solutions = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $solutionOptions = []; |
25
|
|
|
|
26
|
26 |
|
public function addSolution($name, $solution, $options = []) |
27
|
|
|
{ |
28
|
26 |
|
if (!$this->supportsSolution($solution)) { |
29
|
1 |
|
throw new UnsupportedResolverException( |
30
|
1 |
|
sprintf('Resolver "%s" must be "%s" "%s" given.', $name, $this->supportedSolutionClass(), get_class($solution)) |
31
|
1 |
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
26 |
|
$this->solutions[$name] = $solution; |
35
|
26 |
|
$this->solutionOptions[$name] = $options; |
36
|
|
|
|
37
|
26 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $name |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
44 |
|
public function getSolution($name) |
46
|
|
|
{ |
47
|
44 |
|
return isset($this->solutions[$name]) ? $this->solutions[$name] : null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
7 |
|
public function getSolutions() |
54
|
|
|
{ |
55
|
7 |
|
return $this->solutions; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $name |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
31 |
|
public function getSolutionOptions($name) |
64
|
|
|
{ |
65
|
31 |
|
return isset($this->solutionOptions[$name]) ? $this->solutionOptions[$name] : []; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $solution |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
26 |
|
protected function supportsSolution($solution) |
74
|
|
|
{ |
75
|
26 |
|
$supportedClass = $this->supportedSolutionClass(); |
76
|
|
|
|
77
|
26 |
|
return null === $supportedClass || $solution instanceof $supportedClass; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* default return null to accept mixed type. |
82
|
|
|
* |
83
|
|
|
* @return null|string supported class name |
84
|
|
|
*/ |
85
|
13 |
|
protected function supportedSolutionClass() |
86
|
|
|
{ |
87
|
13 |
|
return; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|