Lines 80.00% 4 / 5
Functions and Methods 66.66% 2 / 3
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ComponentInstance 80.00% 4 / 5 66.66% 2 / 3 4.13 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 parse 100.00% 3 / 3 100.00% 1 / 1 2
 __toString 0.00% 0 / 1 0.00% 0 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Client\Component;
6
7use Stewart\Client\Exception\HaClientError;
8use Stewart\Client\Exception\HaClientException;
9use Stringable;
10
11final readonly class ComponentInstance implements Stringable
12{
13    private const string PATTERN = '/\A[a-z][a-z0-9_]{0,63}\z/';
14
15    private function __construct(public string $value) {}
16
17    /** @throws HaClientException */
18    public static function parse(string $value): self
19    {
20        if (preg_match(self::PATTERN, $value) !== 1) {
21            throw HaClientException::createForReason(HaClientError::ComponentInstanceInvalid, instance: $value);
22        }
23
24        return new self($value);
25    }
26
27    public function __toString(): string
28    {
29        return $this->value;
30    }
31}