Total Complexity | 8 |
Total Lines | 66 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
12 | class OptionValues |
||
13 | { |
||
14 | protected $options = []; |
||
15 | |||
16 | protected $separator = '||'; |
||
17 | |||
18 | protected $value_separator = '=='; |
||
19 | |||
20 | /** |
||
21 | * OptionValues constructor. |
||
22 | * @param string $separator ~ the separator between items |
||
23 | * @param string $value_separator ~ the separator between label and value |
||
24 | */ |
||
25 | public function __construct(string $separator='||', string $value_separator='==') |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return string |
||
33 | */ |
||
34 | public function toString() |
||
35 | { |
||
36 | $string = ''; |
||
37 | foreach ($this->options as $label => $value) { |
||
38 | if (!empty($string)) { |
||
39 | $string .= $this->separator; |
||
40 | } |
||
41 | $string .= $label . $this->value_separator . $value; |
||
42 | } |
||
43 | |||
44 | return $string; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return array |
||
49 | */ |
||
50 | public function getOptions(): array |
||
51 | { |
||
52 | return $this->options; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param string $label |
||
57 | * @param null $value |
||
|
|||
58 | * @return $this |
||
59 | */ |
||
60 | public function setOption(string $label, $value=null): self |
||
61 | { |
||
62 | if (is_null($value)) { |
||
63 | $value = $label; |
||
64 | } |
||
65 | $this->options[$label] = $value; |
||
66 | return $this; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * The __toString method allows a class to decide how it will react when it is converted to a string. |
||
71 | * |
||
72 | * @return string |
||
73 | * @link https://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring |
||
74 | */ |
||
75 | public function __toString() |
||
78 | } |
||
79 | } |