Options::getOptions()   A
last analyzed

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
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Common;
13
14
use Bluz\Collection\Collection;
0 ignored issues
show
Bug introduced by
The type Bluz\Collection\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Options Trait
18
 *
19
 * Example of usage
20
 *     class Foo
21
 *     {
22
 *       use \Bluz\Common\Options;
23
 *
24
 *       protected $bar = '';
25
 *       protected $baz = '';
26
 *
27
 *       public function setBar($value)
28
 *       {
29
 *           $this->bar = $value;
30
 *       }
31
 *
32
 *       public function setBaz($value)
33
 *       {
34
 *           $this->baz = $value;
35
 *       }
36
 *     }
37
 *
38
 *     $Foo = new Foo();
39
 *     $Foo->setOptions(['bar'=>123, 'baz'=>456]);
40
 *
41
 * @package  Bluz\Common
42
 * @author   Anton Shevchuk
43
 * @link     https://github.com/bluzphp/framework/wiki/Trait-Options
44
 */
45
trait Options
46
{
47
    /**
48
     * @var array options store
49
     */
50
    protected $options = [];
51
52
    /**
53
     * Get option by key
54
     *
55
     * @param  string $key
56
     * @param  array  $keys
57
     *
58
     * @return mixed
59
     */
60 10
    public function getOption(string $key, ...$keys)
61
    {
62 10
        $method = 'get' . Str::toCamelCase($key);
63 10
        if (method_exists($this, $method)) {
64 1
            return $this->$method($key, ...$keys);
65
        }
66 10
        return Collection::get($this->options, $key, ...$keys);
67
    }
68
69
    /**
70
     * Set option by key over setter
71
     *
72
     * @param  string $key
73
     * @param  mixed $value
74
     *
75
     * @return void
76
     */
77 591
    public function setOption(string $key, $value): void
78
    {
79 591
        $method = 'set' . Str::toCamelCase($key);
80 591
        if (method_exists($this, $method)) {
81 590
            $this->$method($value);
82
        } else {
83 591
            $this->options[$key] = $value;
84
        }
85 591
    }
86
87
    /**
88
     * Get all options
89
     *
90
     * @return array
91
     */
92 1
    public function getOptions(): array
93
    {
94 1
        return $this->options;
95
    }
96
97
    /**
98
     * Setup, check and init options
99
     *
100
     * Requirements
101
     * - options must be a array
102
     * - options can be null
103
     *
104
     * @param array|null $options
105
     *
106
     * @return void
107
     */
108 591
    public function setOptions(?array $options = null): void
109
    {
110
        // store options by default
111 591
        $this->options = (array)$options;
112
113
        // apply options
114 591
        foreach ($this->options as $key => $value) {
115 591
            $this->setOption($key, $value);
116
        }
117 591
    }
118
}
119