Passed
Push — master ( 64ae59...2a8024 )
by Tom
02:55
created

Element   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 89
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setOptions() 0 4 1
A getOptions() 0 4 1
render() 0 1 ?
A toString() 0 4 1
A __toString() 0 4 1
A optionsResolver() 0 21 4
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->setOptions($options);
51
    }
52
53
    public function setOptions(array $options)
54
    {
55
        $this->options = $this->optionsResolver()->resolve($options);
56
    }
57
58
    public function getOptions(): array
59
    {
60
        return $this->options;
61
    }
62
63
    abstract public function render(): string;
64
65
    public function toString(): string
66
    {
67
        return $this->render();
68
    }
69
70
    public function __toString(): string
71
    {
72
        return $this->toString();
73
    }
74
75
    protected function optionsResolver(): OptionsResolver
76
    {
77
        $resolver = new OptionsResolver();
78
        $resolver->setDefaults($this->defaults);
79
80
        $resolver->setRequired($this->required);
81
82
        foreach ($this->types as $option => $types) {
83
            $resolver->setAllowedTypes($option, $types);
84
        }
85
86
        foreach ($this->values as $option => $values) {
87
            $resolver->setAllowedValues($option, $values);
88
        }
89
90
        if (method_exists($this, 'configureOptions')) {
91
            $this->configureOptions($resolver);
92
        }
93
94
        return $resolver;
95
    }
96
}
97