Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
OptionsTrait
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOption
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getOptions
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 setOption
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 setOptions
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Config
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Config;
16
17/**
18 * Options trait.
19 *
20 * This trait basically implements the OptionsInterface
21 *
22 * @package phpOMS\Config
23 * @license OMS License 2.0
24 * @link    https://jingga.app
25 * @since   1.0.0
26 */
27trait OptionsTrait
28{
29    /**
30     * Options.
31     *
32     * @var array
33     * @since 1.0.0
34     */
35    private array $options = [];
36
37    /**
38     * Is this key set.
39     *
40     * @param int|string $key Key to check for existence
41     *
42     * @return bool
43     *
44     * @since 1.0.0
45     */
46    public function exists(int | string $key) : bool
47    {
48        return isset($this->options[$key]);
49    }
50
51    /**
52     * Get option by key.
53     *
54     * @param int|string $key Unique option key
55     *
56     * @return mixed Option value
57     *
58     * @since 1.0.0
59     */
60    public function getOption(int | string $key) : mixed
61    {
62        return $this->options[$key] ?? null;
63    }
64
65    /**
66     * Get options by keys.
67     *
68     * @param array<int, int|string> $key Unique option key
69     *
70     * @return array Option values
71     *
72     * @since 1.0.0
73     */
74    public function getOptions(array $key) : array
75    {
76        $options = [];
77
78        foreach ($key as $value) {
79            if (isset($this->options[$value])) {
80                $options[$value] = $this->options[$value];
81            }
82        }
83
84        return $options;
85    }
86
87    /**
88     * Updating or adding settings.
89     *
90     * @param int|string $key       Unique option key
91     * @param mixed      $value     Option value
92     * @param bool       $overwrite Overwrite existing value
93     *
94     * @return bool
95     *
96     * @since 1.0.0
97     */
98    public function setOption(int | string $key, mixed $value, bool $overwrite = true) : bool
99    {
100        if ($overwrite || !isset($this->options[$key])) {
101            $this->options[$key] = $value;
102
103            return true;
104        }
105
106        return false;
107    }
108
109    /**
110     * Updating or adding settings.
111     *
112     * @param array $pair      Key value pair
113     * @param bool  $overwrite Overwrite existing value
114     *
115     * @return bool
116     *
117     * @since 1.0.0
118     */
119    public function setOptions(array $pair, bool $overwrite = true) : bool
120    {
121        if ($overwrite) {
122            $this->options = $pair + $this->options;
123        } else {
124            $this->options += $pair;
125        }
126
127        return true;
128    }
129}