|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Psi\Component\ContentType\OptionsResolver; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Option resolver that allows options to be designated as either for the |
|
12
|
|
|
* content-type form, or the content-type view. |
|
13
|
|
|
* |
|
14
|
|
|
* All options that are not assigned will be to both the view and the form. |
|
15
|
|
|
*/ |
|
16
|
|
|
class FieldOptionsResolver extends OptionsResolver |
|
17
|
|
|
{ |
|
18
|
|
|
private $formMapper; |
|
19
|
|
|
private $viewMapper; |
|
20
|
|
|
private $storageMapper; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Set closure which will map the resolved field options to form options. |
|
24
|
|
|
*/ |
|
25
|
|
|
public function setFormMapper(\Closure $optionMapper) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->formMapper = $optionMapper; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set closure which will map the resolved field options to view options. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setViewMapper(\Closure $optionMapper) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->viewMapper = $optionMapper; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Set closure which will map the resolved field options to storage options. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setStorageMapper(\Closure $optionMapper) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->storageMapper = $optionMapper; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function resolveFormOptions(array $options = []): array |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->resolveOptions($this->formMapper, $options); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function resolveViewOptions(array $options = []): array |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->resolveOptions($this->viewMapper, $options); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function resolveStorageOptions(array $options = []): array |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->resolveOptions($this->storageMapper, $options); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function resolveOptions($mapper, array $options): array |
|
62
|
|
|
{ |
|
63
|
|
|
if (!$mapper) { |
|
64
|
|
|
return []; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$options = $this->resolve($options); |
|
68
|
|
|
|
|
69
|
|
|
return $mapper($options); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|