forked from sstok/gush-experiments-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResolvedFieldType.php
221 lines (191 loc) · 5.88 KB
/
ResolvedFieldType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <s.stok@rollerscapes.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search;
use Rollerworks\Component\Search\Exception\InvalidArgumentException;
use Rollerworks\Component\Search\Exception\UnexpectedTypeException;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
*/
class ResolvedFieldType implements ResolvedFieldTypeInterface
{
/**
* @var FieldTypeInterface
*/
private $innerType;
/**
* @var FieldTypeExtensionInterface[]
*/
private $typeExtensions;
/**
* @var ResolvedFieldType
*/
private $parent;
/**
* @var OptionsResolver
*/
private $optionsResolver;
/**
* Constructor.
*
* @param FieldTypeInterface $innerType
* @param array $typeExtensions
* @param ResolvedFieldTypeInterface $parent
*
* @throws UnexpectedTypeException When at least one of the given extensions is not an FieldTypeExtensionInterface
* @throws InvalidArgumentException When the name of inner type is invalid
*/
public function __construct(FieldTypeInterface $innerType, array $typeExtensions = [], ResolvedFieldTypeInterface $parent = null)
{
if (!preg_match('/^[a-z0-9_]*$/i', $innerType->getName())) {
throw new InvalidArgumentException(
sprintf(
'The "%s" field-type name ("%s") is not valid. Names must only contain letters, numbers, and "_".',
get_class($innerType),
$innerType->getName()
)
);
}
foreach ($typeExtensions as $extension) {
if (!$extension instanceof FieldTypeExtensionInterface) {
throw new UnexpectedTypeException(
$extension,
'Rollerworks\Component\Search\FieldTypeExtensionInterface'
);
}
}
$this->innerType = $innerType;
$this->typeExtensions = $typeExtensions;
$this->parent = $parent;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return $this->innerType->getName();
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return $this->parent;
}
/**
* {@inheritdoc}
*/
public function getInnerType()
{
return $this->innerType;
}
/**
* {@inheritdoc}
*/
public function getTypeExtensions()
{
return $this->typeExtensions;
}
/**
* {@inheritdoc}
*/
public function createField($name, array $options = [])
{
$options = $this->getOptionsResolver()->resolve($options);
$builder = $this->newField($name, $options);
return $builder;
}
/**
* {@inheritdoc}
*/
public function buildType(FieldConfigInterface $config, array $options)
{
if (null !== $this->parent) {
$this->parent->buildType($config, $options);
}
$this->innerType->buildType($config, $options);
foreach ($this->typeExtensions as $extension) {
$extension->buildType($config, $options);
}
}
/**
* {@inheritdoc}
*/
public function createFieldView(FieldConfigInterface $config)
{
$view = $this->newFieldView($config);
$view->vars = array_merge($view->vars, [
'name' => $config->getName(),
'type' => $config->getType()->getName(),
'accept_ranges' => $config->supportValueType(ValuesBag::VALUE_TYPE_RANGE),
'accept_compares' => $config->supportValueType(ValuesBag::VALUE_TYPE_COMPARISON),
'accept_pattern_matchers' => $config->supportValueType(ValuesBag::VALUE_TYPE_PATTERN_MATCH),
]);
return $view;
}
/**
* {@inheritdoc}
*/
public function buildFieldView(SearchFieldView $view, FieldConfigInterface $config, array $options)
{
if (null !== $this->parent) {
$this->parent->buildFieldView($view, $config, $options);
}
$this->innerType->buildView($view, $config, $options);
foreach ($this->typeExtensions as $extension) {
$extension->buildView($config, $view);
}
}
/**
* {@inheritdoc}
*/
public function getOptionsResolver()
{
if (null === $this->optionsResolver) {
if (null !== $this->parent) {
$this->optionsResolver = clone $this->parent->getOptionsResolver();
} else {
$this->optionsResolver = new OptionsResolver();
}
$this->innerType->configureOptions($this->optionsResolver);
foreach ($this->typeExtensions as $extension) {
$extension->configureOptions($this->optionsResolver);
}
}
return $this->optionsResolver;
}
/**
* Creates a new SearchField instance.
*
* Override this method if you want to customize the field class.
*
* @param string $name The name of the field
* @param array $options The builder options
*
* @return SearchField The new field instance
*/
protected function newField($name, array $options)
{
return new SearchField($name, $this, $options);
}
/**
* Creates a new SearchFieldView instance.
*
* Override this method if you want to customize the view class.
*
* @param FieldConfigInterface $config The search field
*
* @return SearchFieldView The new view instance
*/
protected function newFieldView(FieldConfigInterface $config)
{
return new SearchFieldView($config);
}
}