Lines
55.55%
5 / 9
Functions and Methods
60.00%
3 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ExposedEntityKey | 55.55% | 5 / 9 | 60.00% | 3 / 5 | 16.11 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | |||||
| fromKeyOrString | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | |||||
| tryFromString | 0.00% | 0 / 3 | 0.00% | 0 / 1 | 6 | |||||
| equals | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| __toString | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Contracts\Exposure; | |
| 6 | ||
| 7 | use Stewart\Contracts\Exception\ExposureError; | |
| 8 | use Stewart\Contracts\Exception\ExposureException; | |
| 9 | use Stringable; | |
| 10 | ||
| 11 | final readonly class ExposedEntityKey implements Stringable | |
| 12 | { | |
| 13 | private const string PATTERN = '/\A[a-z][a-z0-9_]*\z/'; | |
| 14 | ||
| 15 | private const int MAX_LENGTH = 64; | |
| 16 | ||
| 17 | public string $value; | |
| 18 | ||
| 19 | /** @throws ExposureException */ | |
| 20 | public function __construct(string $value) | |
| 21 | { | |
| 22 | if (preg_match(self::PATTERN, $value) !== 1 || \strlen($value) > self::MAX_LENGTH) { | |
| 23 | throw ExposureException::createForReason(ExposureError::KeyInvalid, key: $value, limit: self::MAX_LENGTH); | |
| 24 | } | |
| 25 | ||
| 26 | $this->value = $value; | |
| 27 | } | |
| 28 | ||
| 29 | /** @throws ExposureException */ | |
| 30 | public static function fromKeyOrString(self|string $key): self | |
| 31 | { | |
| 32 | return $key instanceof self ? $key : new self($key); | |
| 33 | } | |
| 34 | ||
| 35 | public static function tryFromString(string $value): ?self | |
| 36 | { | |
| 37 | try { | |
| 38 | return new self($value); | |
| 39 | } catch (ExposureException) { | |
| 40 | return null; | |
| 41 | } | |
| 42 | } | |
| 43 | ||
| 44 | public function equals(self $other): bool | |
| 45 | { | |
| 46 | return $this->value === $other->value; | |
| 47 | } | |
| 48 | ||
| 49 | public function __toString(): string | |
| 50 | { | |
| 51 | return $this->value; | |
| 52 | } | |
| 53 | } |