Completed
Pull Request — master (#4)
by Tom
02:56
created

Element::isValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Curlyspoon\Core\Elements;
4
5
use Curlyspoon\Core\Contracts\Element as ElementContract;
6
use Exception;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
9
abstract class Element implements ElementContract
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $name;
15
16
    /**
17
     * @var array
18
     *
19
     * @see OptionsResolver::setDefault()
20
     */
21
    protected $defaults = [];
22
23
    /**
24
     * @var array
25
     *
26
     * @see OptionsResolver::setRequired()
27
     */
28
    protected $required = [];
29
30
    /**
31
     * @var array
32
     *
33
     * @see OptionsResolver::setAllowedTypes()
34
     */
35
    protected $types = [];
36
37
    /**
38
     * @var array
39
     *
40
     * @see OptionsResolver::setAllowedValues()
41
     */
42
    protected $values = [];
43
44
    /**
45
     * @var array
46
     */
47
    protected $options = [];
48
49
    public function __construct(array $options = [])
50
    {
51
        $this->setOptions($options);
52
    }
53
54
    public static function make(array $options = []): ElementContract
55
    {
56
        return new static($options);
57
    }
58
59
    public function setOptions(array $options): ElementContract
60
    {
61
        $this->options = $options;
62
63
        return $this;
64
    }
65
66
    public function getOptions(): array
67
    {
68
        return $this->options;
69
    }
70
71
    public function resolve(): ElementContract
72
    {
73
        $this->setOptions($this->optionsResolver()->resolve($this->getOptions()));
74
75
        return $this;
76
    }
77
78
    public function isValid(): bool
79
    {
80
        try {
81
            $this->optionsResolver()->resolve($this->getOptions());
82
83
            return true;
84
        } catch (Exception $exception) {
85
            return false;
86
        }
87
    }
88
89
    abstract public function render(): string;
90
91
    public function toString(): string
92
    {
93
        return $this->render();
94
    }
95
96
    public function __toString(): string
97
    {
98
        return $this->toString();
99
    }
100
101
    protected function optionsResolver(): OptionsResolver
102
    {
103
        $resolver = new OptionsResolver();
104
        $resolver->setDefaults($this->defaults);
105
106
        $resolver->setRequired($this->required);
107
108
        foreach ($this->types as $option => $types) {
109
            $resolver->setAllowedTypes($option, $types);
110
        }
111
112
        foreach ($this->values as $option => $values) {
113
            $resolver->setAllowedValues($option, $values);
114
        }
115
116
        if (method_exists($this, 'configureOptions')) {
117
            $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...
118
        }
119
120
        return $resolver;
121
    }
122
}
123