FieldOptionsResolver::setStorageMapper()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType\OptionsResolver;
6
7
use Psi\Component\ContentType\FieldOptions;
8
use Symfony\Component\OptionsResolver\Options;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * Option resolver that allows options to be designated as either for the
13
 * content-type form, or the content-type view.
14
 *
15
 * All options that are not assigned will be to both the view and the form.
16
 */
17
class FieldOptionsResolver extends OptionsResolver
18
{
19
    private $formMapper;
20
    private $viewMapper;
21
    private $storageMapper;
22
23
    /**
24
     * Set closure which will map the resolved field options to form options.
25
     */
26
    public function setFormMapper(\Closure $optionMapper)
27
    {
28
        $this->formMapper = $optionMapper;
29
    }
30
31
    /**
32
     * Set closure which will map the resolved field options to view options.
33
     */
34
    public function setViewMapper(\Closure $optionMapper)
35
    {
36
        $this->viewMapper = $optionMapper;
37
    }
38
39
    /**
40
     * Set closure which will map the resolved field options to storage options.
41
     */
42
    public function setStorageMapper(\Closure $optionMapper)
43
    {
44
        $this->storageMapper = $optionMapper;
45
    }
46
47
    public function resolveFormOptions(FieldOptions $options): array
48
    {
49
        return $this->resolveOptions($this->formMapper, $options->getSharedOptions(), $options->getFormOptions());
50
    }
51
52
    public function resolveViewOptions(FieldOptions $options): array
53
    {
54
        return $this->resolveOptions($this->viewMapper, $options->getSharedOptions(), $options->getViewOptions());
55
    }
56
57
    public function resolveStorageOptions(FieldOptions $options): array
58
    {
59
        return $this->resolveOptions($this->storageMapper, $options->getSharedOptions(), $options->getViewOptions());
60
    }
61
62
    private function resolveOptions($mapper, array $sharedOptions, array $typeOptions): array
63
    {
64
        // if no mapper was specified, then pass all of the type options
65
        // directly.
66
        if (!$mapper) {
67
            return $typeOptions;
68
        }
69
70
        // otherwise use the mapper callback, passing both type and shared
71
        // options.
72
        $options = $this->resolve($sharedOptions);
73
74
        return $mapper($typeOptions, $options);
75
    }
76
}
77