Completed
Pull Request — master (#399)
by Anton
04:06
created

Options   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 104
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getOption() 0 12 3
A setOption() 0 9 2
A getOptions() 0 4 1
A setOptions() 0 15 2
A initOptions() 0 4 1
A normalizeKey() 0 6 1
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Common;
12
13
/**
14
 * Options Trait
15
 *
16
 * Example of usage
17
 *     class Foo
18
 *     {
19
 *       use \Bluz\Common\Options;
20
 *
21
 *       protected $bar = '';
22
 *       protected $baz = '';
23
 *
24
 *       public function setBar($value)
25
 *       {
26
 *           $this->bar = $value;
27
 *       }
28
 *
29
 *       public function setBaz($value)
30
 *       {
31
 *           $this->baz = $value;
32
 *       }
33
 *     }
34
 *
35
 *     $Foo = new Foo(['bar'=>123, 'baz'=>456]);
36
 *
37
 * @package  Bluz\Common
38
 * @author   Anton Shevchuk
39
 * @link     https://github.com/bluzphp/framework/wiki/Trait-Options
40
 */
41
trait Options
42
{
43
    /**
44
     * @var array options store
45
     */
46
    protected $options;
47
48
    /**
49
     * Get option by key
50
     *
51
     * @param  string      $key
52
     * @param  string|null $section
53
     * @return mixed
54
     */
55
    public function getOption($key, $section = null)
56
    {
57
        if (isset($this->options[$key])) {
58
            if (!is_null($section)) {
59
                return $this->options[$key][$section] ?? null;
60
            } else {
61
                return $this->options[$key];
62
            }
63
        } else {
64
            return null;
65
        }
66
    }
67
68
    /**
69
     * Set option by key over setter
70
     *
71
     * @param  string $key
72
     * @param  string $value
73
     * @return void
74
     */
75
    public function setOption($key, $value)
76
    {
77
        $method = 'set' . $this->normalizeKey($key);
78
        if (method_exists($this, $method)) {
79
            $this->$method($value);
80
        } else {
81
            $this->options[$key] = $value;
82
        }
83
    }
84
85
    /**
86
     * Get all options
87
     *
88
     * @return array
89
     */
90
    public function getOptions() : array
91
    {
92
        return $this->options;
93
    }
94
95
    /**
96
     * Setup, check and init options
97
     *
98
     * Requirements
99
     * - options must be a array
100
     * - options can be null
101
     *
102
     * @param  array $options
103
     * @return self
104
     */
105
    public function setOptions(array $options = null)
106
    {
107
        // store options by default
108
        $this->options = (array) $options;
109
110
        // apply options
111
        foreach ($this->options as $key => $value) {
112
            $this->setOption($key, $value);
113
        }
114
115
        // check and initialize options
116
        $this->initOptions();
117
118
        return $this;
119
    }
120
121
    /**
122
     * Check and initialize options in package
123
     *
124
     * @throws \Bluz\Config\ConfigException
125
     * @return void
126
     */
127
    protected function initOptions()
128
    {
129
        return;
130
    }
131
132
    /**
133
     * Normalize key name
134
     *
135
     * @param  string $key
136
     * @return string
137
     */
138
    private function normalizeKey($key)
139
    {
140
        $option = str_replace(['_', '-'], ' ', strtolower($key));
141
        $option = str_replace(' ', '', ucwords($option));
142
        return $option;
143
    }
144
}
145