forked from DZunke/FeatureFlagsBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Toggle.php
77 lines (62 loc) · 1.28 KB
/
Toggle.php
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
<?php
namespace DZunke\FeatureFlagsBundle;
use DZunke\FeatureFlagsBundle\Toggle\Flag;
class Toggle
{
/**
* @var bool
*/
private $defaultState = true;
/**
* @var Flag[]
*/
private $flags = [];
/**
* @param bool $defaultState
* @return $this
*/
public function setDefaultState($defaultState): self
{
$this->defaultState = (bool)$defaultState;
return $this;
}
/**
* @param Flag $flag
* @return $this
*/
public function addFlag(Flag $flag): self
{
$this->flags[(string)$flag] = $flag;
return $this;
}
/**
* @param $name
* @return Flag|null
*/
public function getFlag($name): ?Flag
{
if (isset($this->flags[$name])) {
return $this->flags[$name];
}
return null;
}
/**
* @return Toggle\Flag[]
*/
public function getFlags(): array
{
return $this->flags;
}
/**
* @param string $flag
* @param array $arguments
* @return bool
*/
public function isActive($flag, $arguments = null): bool
{
if (!isset($this->flags[$flag])) {
return $this->defaultState;
}
return $this->flags[$flag]->isActive($arguments);
}
}