Completed
Pull Request — master (#457)
by Anton
11:41
created

Options::initOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
     *
55
     * @return mixed
56
     */
57 10
    public function getOption($key, ...$keys)
58
    {
59 10
        $method = 'get' . Str::toCamelCase($key);
60 10
        if (method_exists($this, $method)) {
61 1
            return $this->$method($key, ...$keys);
62
        }
63 10
        return Collection::get($this->options, $key, ...$keys);
64
    }
65
66
    /**
67
     * Set option by key over setter
68
     *
69
     * @param  string $key
70
     * @param  string $value
71
     *
72
     * @return void
73
     */
74 596
    public function setOption($key, $value) : void
75
    {
76 596
        $method = 'set' . Str::toCamelCase($key);
77 596
        if (method_exists($this, $method)) {
78 595
            $this->$method($value);
79
        } else {
80 591
            $this->options[$key] = $value;
81
        }
82 596
    }
83
84
    /**
85
     * Get all options
86
     *
87
     * @return array
88
     */
89 1
    public function getOptions() : array
90
    {
91 1
        return $this->options;
92
    }
93
94
    /**
95
     * Setup, check and init options
96
     *
97
     * Requirements
98
     * - options must be a array
99
     * - options can be null
100
     *
101
     * @param  array $options
102
     *
103
     * @return void
104
     */
105 596
    public function setOptions(array $options = null) : void
106
    {
107
        // store options by default
108 596
        $this->options = (array)$options;
109
110
        // apply options
111 596
        foreach ($this->options as $key => $value) {
112 596
            $this->setOption($key, $value);
113
        }
114
    }
115
}
116