Completed
Push — master ( 38ab68...6dcf03 )
by Tom
06:00 queued 01:56
created

Element::getOptions()   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
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
    /**
11
     * @var string
12
     */
13
    protected $name;
14
15
    /**
16
     * @var array
17
     *
18
     * @see OptionsResolver::setDefault()
19
     */
20
    protected $defaults = [];
21
22
    /**
23
     * @var array
24
     *
25
     * @see OptionsResolver::setRequired()
26
     */
27
    protected $required = [];
28
29
    /**
30
     * @var array
31
     *
32
     * @see OptionsResolver::setAllowedTypes()
33
     */
34
    protected $types = [];
35
36
    /**
37
     * @var array
38
     *
39
     * @see OptionsResolver::setAllowedValues()
40
     */
41
    protected $values = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $options = [];
47
48
    public function __construct(array $options = [])
49
    {
50
        $this->options = $this->optionsResolver()->resolve($options);
51
    }
52
53
    public function getOptions(): array
54
    {
55
        return $this->options;
56
    }
57
58
    abstract public function render(): string;
59
60
    public function toString(): string
61
    {
62
        return $this->render();
63
    }
64
65
    public function __toString(): string
66
    {
67
        return $this->toString();
68
    }
69
70
    protected function optionsResolver(): OptionsResolver
71
    {
72
        $resolver = new OptionsResolver();
73
        $resolver->setDefaults($this->defaults);
74
75
        $resolver->setRequired($this->required);
76
77
        foreach ($this->types as $option => $types) {
78
            $resolver->setAllowedTypes($option, $types);
79
        }
80
81
        foreach ($this->values as $option => $values) {
82
            $resolver->setAllowedValues($option, $values);
83
        }
84
85
        if (method_exists($this, 'configureOptions')) {
86
            $this->configureOptions($resolver);
1 ignored issue
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...
87
        }
88
89
        return $resolver;
90
    }
91
}
92