Lines 100.00% 4 / 4
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ExposedEntityConfig 100.00% 4 / 4 100.00% 1 / 1 4 100.00% 1 / 1
 __construct 100.00% 4 / 4 100.00% 1 / 1 4
 getPlatform n/a 0 / 0 n/a 0 / 0 0
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Exposure;
6
7use Stewart\Contracts\Exception\ExposureException;
8
9abstract readonly class ExposedEntityConfig implements ExposureConfig
10{
11    private const string ICON_PATTERN = '/\Amdi:[a-z0-9-]+\z/';
12
13    /** @throws ExposureException */
14    public function __construct(
15        public ?string $name,
16        public ?string $icon,
17        public ?EntityCategory $entityCategory,
18        public bool $enabledByDefault,
19    ) {
20        if ($name === '') {
21            throw ExposureException::configInvalid('The entity name must not be empty; leave it null to use the device name.');
22        }
23
24        if ($icon !== null && preg_match(self::ICON_PATTERN, $icon) !== 1) {
25            throw ExposureException::configInvalid(\sprintf('The icon "%s" must look like mdi:thermometer.', $icon));
26        }
27    }
28
29    abstract public function getPlatform(): ExposedPlatform;
30}