Completed
Push — master ( 57ba8c...1f04bf )
by Daniel
05:57 queued 02:37
created

FieldOptionsResolver   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 56
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFormMapper() 0 4 1
A setViewMapper() 0 4 1
A setStorageMapper() 0 4 1
A resolveFormOptions() 0 4 1
A resolveViewOptions() 0 4 1
A resolveStorageOptions() 0 4 1
A resolveOptions() 0 10 2
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