-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feature.php
35 lines (29 loc) · 974 Bytes
/
Feature.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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\FeatureToggle;
use SonsOfPHP\Component\FeatureToggle\Exception\InvalidArgumentException;
use SonsOfPHP\Contract\FeatureToggle\ContextInterface;
use SonsOfPHP\Contract\FeatureToggle\FeatureInterface;
use SonsOfPHP\Contract\FeatureToggle\ToggleInterface;
/**
* @author Joshua Estes <[email protected]>
*/
final readonly class Feature implements FeatureInterface
{
public function __construct(
private string $key,
private ToggleInterface $toggle,
) {
if (1 === preg_match('/[^A-Za-z0-9_.]/', $key)) {
throw new InvalidArgumentException(sprintf('The key "%s" is invalid. Only "A-Z", "a-z", "0-9", "_", and "." are allowed.', $key));
}
}
public function getKey(): string
{
return $this->key;
}
public function isEnabled(?ContextInterface $context = null): bool
{
return $this->toggle->isEnabled($context);
}
}