Field::getFormOptions()   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 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType;
6
7
use Psi\Component\ContentType\OptionsResolver\FieldOptionsResolver;
8
9
class Field
10
{
11
    private $resolved;
12
    private $resolver;
13
    private $field;
14
    private $options;
15
16
    public function __construct(
17
        FieldInterface $field,
18
        FieldOptions $options
19
    ) {
20
        $this->field = $field;
21
        $this->options = $options;
22
    }
23
24
    public function getOptions(): array
25
    {
26
        return $this->getResolver()->resolve($this->options->getSharedOptions());
27
    }
28
29
    public function getFormType(): string
30
    {
31
        return $this->field->getFormType();
32
    }
33
34
    public function getFormOptions(): array
35
    {
36
        return $this->resolve('resolveFormOptions');
37
    }
38
39
    public function getViewType(): string
40
    {
41
        return $this->field->getViewType();
42
    }
43
44
    public function getViewOptions(): array
45
    {
46
        return $this->resolve('resolveViewOptions');
47
    }
48
49
    public function getStorageOptions(): array
50
    {
51
        return $this->resolve('resolveStorageOptions');
52
    }
53
54
    public function getStorageType(): string
55
    {
56
        return $this->field->getStorageType();
57
    }
58
59
    public function getInnerField()
60
    {
61
        return $this->field;
62
    }
63
64
    private function getResolver(): FieldOptionsResolver
65
    {
66
        if ($this->resolver) {
67
            return $this->resolver;
68
        }
69
70
        $this->resolver = new FieldOptionsResolver();
71
        $this->field->configureOptions($this->resolver);
72
73
        return $this->resolver;
74
    }
75
76
    private function resolve(string $methodName)
77
    {
78
        if (isset($this->resolved[$methodName])) {
79
            return $this->resolved[$methodName];
80
        }
81
82
        $resolver = $this->getResolver();
83
84
        $this->resolved[$methodName] = $resolver->$methodName($this->options);
85
86
        return $this->resolved[$methodName];
87
    }
88
}
89