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

Field::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
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 $options;
14
    private $field;
15
16
    public function __construct(FieldInterface $field, array $options)
17
    {
18
        $this->field = $field;
19
        $this->options = $options;
20
    }
21
22
    public function getOptions(): array
23
    {
24
        return $this->resolve('resolve');
25
    }
26
27
    public function getFormType(): string
28
    {
29
        return $this->field->getFormType();
30
    }
31
32
    public function getFormOptions(): array
33
    {
34
        return $this->resolve('resolveFormOptions');
35
    }
36
37
    public function getViewType(): string
38
    {
39
        return $this->field->getViewType();
40
    }
41
42
    public function getViewOptions(): array
43
    {
44
        return $this->resolve('resolveViewOptions');
45
    }
46
47
    public function getStorageOptions(): array
48
    {
49
        return $this->resolve('resolveStorageOptions');
50
    }
51
52
    public function getStorageType(): string
53
    {
54
        return $this->field->getStorageType();
55
    }
56
57
    private function getResolver(): FieldOptionsResolver
58
    {
59
        if ($this->resolver) {
60
            return $this->resolver;
61
        }
62
63
        $this->resolver = new FieldOptionsResolver();
64
        $this->field->configureOptions($this->resolver);
65
66
        return $this->resolver;
67
    }
68
69
    private function resolve(string $methodName)
70
    {
71
        if (isset($this->resolved[$methodName])) {
72
            return $this->resolved[$methodName];
73
        }
74
75
        $resolver = $this->getResolver();
76
77
        $this->resolved[$methodName] = $resolver->$methodName($this->options);
78
79
        return $this->resolved[$methodName];
80
    }
81
}
82