Completed
Pull Request — master (#278)
by Piotr
02:47
created

AbstractElement::getOption()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminBundle\Admin;
11
12
use FSi\Bundle\AdminBundle\Exception\MissingOptionException;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
/**
16
 * @author Norbert Orzechowicz <[email protected]>
17
 */
18
abstract class AbstractElement implements Element
19
{
20
    /**
21
     * @var array
22
     */
23
    private $options;
24
25
    /**
26
     * @var array
27
     */
28
    private $unresolvedOptions;
29
30
    /**
31
     * @param array $options
32
     */
33
    public function __construct(array $options = [])
34
    {
35
        $this->unresolvedOptions = $options;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getRouteParameters()
42
    {
43
        return [
44
            'element' => $this->getId(),
45
        ];
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getOption($name)
52
    {
53
        $this->initOptions();
54
        if (!$this->hasOption($name)) {
55
            throw new MissingOptionException(sprintf('Option with name: "%s" does\'t exists.', $name));
56
        }
57
58
        return $this->options[$name];
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getOptions()
65
    {
66
        $this->initOptions();
67
        return $this->options;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function hasOption($name)
74
    {
75
        $this->initOptions();
76
        return isset($this->options[$name]);
77
    }
78
79
    private function initOptions()
80
    {
81
        if (!is_array($this->options)) {
82
            $optionsResolver = new OptionsResolver();
83
            $this->setDefaultOptions($optionsResolver);
84
            $this->options = $optionsResolver->resolve($this->unresolvedOptions);
85
            unset($this->unresolvedOptions);
86
        }
87
    }
88
}
89