Element::setOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Curlyspoon\Core\Elements;
4
5
use Curlyspoon\Core\Contracts\Element as ElementContract;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
abstract class Element implements ElementContract
9
{
10
    const TYPE_SCALAR = 'scalar';
11
    const TYPE_STRING = 'string';
12
    const TYPE_BOOL = 'bool';
13
14
    const TYPE_INT = 'int';
15
    const TYPE_LONG = 'long';
16
17
    const TYPE_FLOAT = 'float';
18
    const TYPE_DOUBLE = 'double';
19
    const TYPE_REAL = 'real';
20
21
    const TYPE_NUMERIC = 'numeric';
22
    const TYPE_NAN = 'nan';
23
    const TYPE_FINITE = 'finite';
24
    const TYPE_INFINITE = 'infinite';
25
26
    const TYPE_NULL = 'null';
27
    const TYPE_ARRAY = 'array';
28
    const TYPE_OBJECT = 'object';
29
    const TYPE_CALLABLE = 'callable';
30
    const TYPE_COUNTABLE = 'countable';
31
    const TYPE_ITERABLE = 'iterable';
32
    const TYPE_RESOURCE = 'resource';
33
34
    const TYPE_DIR = 'dir';
35
    const TYPE_FILE = 'file';
36
    const TYPE_LINK = 'link';
37
    const TYPE_READABLE = 'readable';
38
    const TYPE_WRITABLE = 'writable';
39
    const TYPE_WRITEABLE = 'writeable';
40
    const TYPE_EXECUTABLE = 'executable';
41
42
    /**
43
     * @var string
44
     */
45
    protected $name;
46
47
    /**
48
     * @var array
49
     *
50
     * @see OptionsResolver::setDefault()
51
     */
52
    protected $defaults = [];
53
54
    /**
55
     * @var array
56
     *
57
     * @see OptionsResolver::setRequired()
58
     */
59
    protected $required = [];
60
61
    /**
62
     * @var array
63
     *
64
     * @see OptionsResolver::setAllowedTypes()
65
     */
66
    protected $types = [];
67
68
    /**
69
     * @var array
70
     *
71
     * @see OptionsResolver::setAllowedValues()
72
     */
73
    protected $values = [];
74
75
    /**
76
     * @var array
77
     */
78
    protected $options = [];
79
80
    public function __construct(array $options = [])
81
    {
82
        $this->setOptions($options);
83
    }
84
85
    public function setOptions(array $options)
86
    {
87
        $this->options = $this->optionsResolver()->resolve($options);
88
    }
89
90
    public function getOptions(): array
91
    {
92
        return $this->options;
93
    }
94
95
    abstract public function render(): string;
96
97
    public function toString(): string
98
    {
99
        return $this->render();
100
    }
101
102
    public function __toString(): string
103
    {
104
        return $this->toString();
105
    }
106
107
    protected function optionsResolver(): OptionsResolver
108
    {
109
        $resolver = new OptionsResolver();
110
        $resolver->setDefaults($this->defaults);
111
112
        $resolver->setRequired($this->required);
113
114
        foreach ($this->types as $option => $types) {
115
            $resolver->setAllowedTypes($option, $types);
116
        }
117
118
        foreach ($this->values as $option => $values) {
119
            $resolver->setAllowedValues($option, $values);
120
        }
121
122
        if (method_exists($this, 'configureOptions')) {
123
            $this->configureOptions($resolver);
0 ignored issues
show
Bug introduced by
The method configureOptions() does not seem to exist on object<Curlyspoon\Core\Elements\Element>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
124
        }
125
126
        return $resolver;
127
    }
128
}
129