Completed
Pull Request — master (#413)
by Anton
14:05
created

Options::normalizeKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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();
36
 *     $Foo->setOptions(['bar'=>123, 'baz'=>456]);
37
 *
38
 * @package  Bluz\Common
39
 * @author   Anton Shevchuk
40
 * @link     https://github.com/bluzphp/framework/wiki/Trait-Options
41
 */
42
trait Options
43
{
44
    /**
45
     * @var array options store
46
     */
47
    protected $options;
48
49
    /**
50
     * Get option by key
51
     *
52
     * @param  string $key
53
     * @param  array  $keys
54
     * @return mixed
55 9
     */
56
    public function getOption($key, ...$keys)
57 9
    {
58 8
        $method = 'get' . Line::toCamelCase($key);
59 7
        if (method_exists($this, $method)) {
60
            return $this->$method($key, ...$keys);
61 2
        }
62
        return Collection::get($this->options, $key, ...$keys);
63
    }
64 2
65
    /**
66
     * Set option by key over setter
67
     *
68
     * @param  string $key
69
     * @param  string $value
70
     * @return void
71
     */
72
    public function setOption($key, $value)
73
    {
74
        $method = 'set' . Line::toCamelCase($key);
75 715
        if (method_exists($this, $method)) {
76
            $this->$method($value);
77 715
        } else {
78 715
            $this->options[$key] = $value;
79 713
        }
80
    }
81 694
82
    /**
83 715
     * Get all options
84
     *
85
     * @return array
86
     */
87
    public function getOptions() : array
88
    {
89
        return $this->options;
90 1
    }
91
92 1
    /**
93
     * Setup, check and init options
94
     *
95
     * Requirements
96
     * - options must be a array
97
     * - options can be null
98
     *
99
     * @param  array $options
100
     * @return self
101
     */
102
    public function setOptions(array $options = null)
103
    {
104
        // store options by default
105 713
        $this->options = (array) $options;
106
107
        // apply options
108 713
        foreach ($this->options as $key => $value) {
109
            $this->setOption($key, $value);
110
        }
111 713
112 713
        // check and initialize options
113
        $this->initOptions();
114
115
        return $this;
116 713
    }
117
118 713
    /**
119
     * Check and initialize options in package
120
     *
121
     * @throws \Bluz\Config\ConfigException
122
     * @return void
123
     */
124
    protected function initOptions()
125
    {
126
    }
127
}
128