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
|
|
|
use Overblog\GraphQLBundle\Resolver\Cache\CacheInterface; |
15
|
|
|
use Overblog\GraphQLBundle\Resolver\Cache\ArrayCache; |
16
|
|
|
|
17
|
|
|
abstract class AbstractResolver implements ResolverInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $solutions = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $solutionOptions = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CacheInterface |
31
|
|
|
*/ |
32
|
|
|
protected $cache; |
33
|
|
|
|
34
|
41 |
|
public function __construct(CacheInterface $cache = null) |
35
|
|
|
{ |
36
|
41 |
|
$this->cache = null !== $cache ? $cache : new ArrayCache(); |
37
|
41 |
|
} |
38
|
|
|
|
39
|
41 |
|
public function addSolution($name, $solution, $options = []) |
40
|
|
|
{ |
41
|
41 |
|
if (!$this->supportsSolution($solution)) { |
42
|
|
|
throw new UnsupportedResolverException( |
43
|
|
|
sprintf('Resolver "%s" must be "%s" "%s" given.', $name, $this->supportedSolutionClass(), get_class($solution)) |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
41 |
|
$this->solutions[$name] = $solution; |
48
|
41 |
|
$this->solutionOptions[$name] = $options; |
49
|
|
|
|
50
|
41 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getSolutions() |
57
|
|
|
{ |
58
|
|
|
return $this->solutions; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $name |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
41 |
|
public function getSolution($name) |
67
|
|
|
{ |
68
|
41 |
|
return isset($this->solutions[$name]) ? $this->solutions[$name] : null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $name |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
17 |
|
public function getSolutionOptions($name) |
77
|
|
|
{ |
78
|
17 |
|
return isset($this->solutionOptions[$name]) ? $this->solutionOptions[$name] : []; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param $input |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
abstract public function resolve($input); |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param mixed $solution |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
41 |
|
protected function supportsSolution($solution) |
94
|
|
|
{ |
95
|
41 |
|
$supportedClass = $this->supportedSolutionClass(); |
96
|
|
|
|
97
|
41 |
|
return null === $supportedClass || $solution instanceof $supportedClass; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* default return null to accept mixed type. |
102
|
|
|
* |
103
|
|
|
* @return null|string supported class name |
104
|
|
|
*/ |
105
|
33 |
|
protected function supportedSolutionClass() |
106
|
|
|
{ |
107
|
33 |
|
return; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|