Completed
Push — master ( 477ca5...2d5ed5 )
by Daniel
10s
created

LoadedField::getViewType()   A

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
use Psi\Component\ContentType\Storage\ConfiguredType;
9
use Psi\Component\ContentType\Storage\TypeFactory;
10
11
/**
12
 * Loaded field wraps the basic field service and lazily provides access to
13
 * its storage mapping and resolved options.
14
 */
15
class LoadedField
16
{
17
    private $resolvedOptions;
18
    private $resolvedFormOptions;
19
    private $resolvedViewOptions;
20
    private $resolvedStorageType;
21
    private $resolver;
22
    private $factory;
23
    private $options;
24
    private $field;
25
26
    public function __construct(TypeFactory $factory, FieldInterface $field, array $options)
27
    {
28
        $this->factory = $factory;
29
        $this->field = $field;
30
        $this->options = $options;
31
    }
32
33
    /**
34
     * Return the resolved options for the field's form type.
35
     */
36
    public function getFormOptions(): array
37
    {
38
        if ($this->resolvedFormOptions) {
39
            return $this->resolvedFormOptions;
40
        }
41
42
        $resolver = $this->getResolver();
43
        $this->resolvedFormOptions = $resolver->resolveFormOptions($this->options);
44
45
        return $this->resolvedFormOptions;
46
    }
47
48
    /**
49
     * Return all of the field's resolved options.
50
     */
51
    public function getOptions(): array
52
    {
53
        if ($this->resolvedOptions) {
54
            return $this->resolvedOptions;
55
        }
56
        $resolver = $this->getResolver();
57
        $this->resolvedOptions = $resolver->resolve($this->options);
58
59
        return $this->resolvedOptions;
60
    }
61
62
    /**
63
     * Return all the resolved options for the field's view.
64
     */
65
    public function getViewOptions(): array
66
    {
67
        if ($this->resolvedViewOptions) {
68
            return $this->resolvedViewOptions;
69
        }
70
71
        $resolver = $this->getResolver();
72
        $this->resolvedViewOptions = $resolver->resolveViewOptions($this->options);
73
74
        return $this->resolvedViewOptions;
75
    }
76
77
    public function getViewType(): string
78
    {
79
        return $this->field->getViewType();
80
    }
81
82
    /**
83
     * Return the configured storage type.
84
     */
85
    public function getStorageType(): ConfiguredType
86
    {
87
        if ($this->resolvedStorageType) {
88
            return $this->resolvedStorageType;
89
        }
90
91
        $this->resolvedStorageType = $this->field->getStorageType($this->factory);
92
93
        return $this->resolvedStorageType;
94
    }
95
96
    /**
97
     * Return the actual field class.
98
     */
99
    public function getInnerField(): FieldInterface
100
    {
101
        return $this->field;
102
    }
103
104
    private function getResolver(): FieldOptionsResolver
105
    {
106
        if ($this->resolver) {
107
            return $this->resolver;
108
        }
109
110
        $this->resolver = new FieldOptionsResolver();
111
        $this->field->configureOptions($this->resolver);
112
113
        return $this->resolver;
114
    }
115
}
116