summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Less/LightMode.php
blob: b4b72a0743b4ce0f0d6f2077c02d30dfa2d12bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */

namespace Icinga\Less;

use ArrayIterator;
use InvalidArgumentException;
use IteratorAggregate;
use Less_Environment;
use Traversable;

/**
 * Registry for light modes and the environments in which they are defined
 */
class LightMode implements IteratorAggregate
{
    /** @var array Mode environments as mode-environment pairs */
    protected $envs = [];

    /** @var array Assoc list of modes */
    protected $modes = [];

    /** @var array Mode selectors as mode-selector pairs */
    protected $selectors = [];

    /**
     * @param string $mode
     *
     * @return $this
     *
     * @throws InvalidArgumentException If the mode already exists
     */
    public function add($mode)
    {
        if (array_key_exists($mode, $this->modes)) {
            throw new InvalidArgumentException("$mode already exists");
        }

        $this->modes[$mode] = true;

        return $this;
    }

    /**
     * @param string $mode
     *
     * @return Less_Environment
     *
     * @throws InvalidArgumentException If there is no environment for the given mode
     */
    public function getEnv($mode)
    {
        if (! isset($this->envs[$mode])) {
            throw new InvalidArgumentException("$mode does not exist");
        }

        return $this->envs[$mode];
    }

    /**
     * @param string           $mode
     * @param Less_Environment $env
     *
     * @return $this
     *
     * @throws InvalidArgumentException If an environment for given the mode already exists
     */
    public function setEnv($mode, Less_Environment $env)
    {
        if (array_key_exists($mode, $this->envs)) {
            throw new InvalidArgumentException("$mode already exists");
        }

        $this->envs[$mode] = $env;

        return $this;
    }

    /**
     * @param string $mode
     *
     * @return bool
     */
    public function hasSelector($mode)
    {
        return isset($this->selectors[$mode]);
    }

    /**
     * @param string $mode
     *
     * @return string
     *
     * @throws InvalidArgumentException If there is no selector for the given mode
     */
    public function getSelector($mode)
    {
        if (! isset($this->selectors[$mode])) {
            throw new InvalidArgumentException("$mode does not exist");
        }

        return $this->selectors[$mode];
    }

    /**
     * @param string $mode
     * @param string $selector
     *
     * @return $this
     *
     * @throws InvalidArgumentException If a selector for given the mode already exists
     */
    public function setSelector($mode, $selector)
    {
        if (array_key_exists($mode, $this->selectors)) {
            throw new InvalidArgumentException("$mode already exists");
        }

        $this->selectors[$mode] = $selector;

        return $this;
    }

    public function getIterator(): Traversable
    {
        return new ArrayIterator(array_keys($this->modes));
    }
}