Passed
Push — master ( 13c395...4d9cd3 )
by Josh
04:14
created

OptionValues   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 3 1
A toString() 0 11 3
A getOptions() 0 3 1
A setOption() 0 7 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: joshgulledge
5
 * Date: 10/26/18
6
 * Time: 11:56 AM
7
 */
8
9
namespace LCI\Blend\Helpers\TVInput;
10
11
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='==')
26
    {
27
        $this->separator = $separator;
28
        $this->value_separator = $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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
58
     * @return $this
59
     */
60
    public function setOption(string $label, $value=null): self
61
    {
62
        if (is_null($value)) {
0 ignored issues
show
introduced by
The condition is_null($value) is always true.
Loading history...
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()
76
    {
77
        return $this->toString();
78
    }
79
}