Lines 100.00% 39 / 39
Functions and Methods 100.00% 6 / 6
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
EntityRegistryEntry 100.00% 39 / 39 100.00% 6 / 6 9 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 isDisabled 100.00% 1 / 1 100.00% 1 / 1 1
 isHidden 100.00% 1 / 1 100.00% 1 / 1 1
 fromArray 100.00% 12 / 12 100.00% 1 / 1 1
 toArray 100.00% 12 / 12 100.00% 1 / 1 3
 toRegisteredEntity 100.00% 12 / 12 100.00% 1 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Client\Registry;
6
7use Stewart\Contracts\Entity\EntityId;
8use Stewart\Contracts\Exception\IdentifierException;
9use Stewart\Contracts\Registry\AreaId;
10use Stewart\Contracts\Registry\DeviceId;
11use Stewart\Contracts\Registry\EntityAlias;
12use Stewart\Contracts\Registry\LabelId;
13use Stewart\Contracts\Registry\RegisteredEntity;
14
15final readonly class EntityRegistryEntry
16{
17    /**
18     * @param list<string> $labelIds
19     * @param list<string|null>|null $aliases
20     */
21    public function __construct(
22        public EntityId $entityId,
23        public ?string $disabledBy = null,
24        public ?string $hiddenBy = null,
25        public ?string $name = null,
26        public ?string $areaId = null,
27        public ?string $deviceId = null,
28        public array $labelIds = [],
29        public ?string $entityCategory = null,
30        public ?string $icon = null,
31        public ?array $aliases = null,
32    ) {}
33
34    public function isDisabled(): bool
35    {
36        return $this->disabledBy !== null;
37    }
38
39    public function isHidden(): bool
40    {
41        return $this->hiddenBy !== null;
42    }
43
44    /**
45     * @param array<array-key, mixed> $raw
46     * @throws IdentifierException
47     */
48    public static function fromArray(array $raw): self
49    {
50        return new self(
51            entityId: new EntityId(RegistryRow::readString($raw, 'entity_id') ?? ''),
52            disabledBy: RegistryRow::readString($raw, 'disabled_by'),
53            hiddenBy: RegistryRow::readString($raw, 'hidden_by'),
54            name: RegistryRow::readString($raw, 'name'),
55            areaId: RegistryRow::readString($raw, 'area_id'),
56            deviceId: RegistryRow::readString($raw, 'device_id'),
57            labelIds: RegistryRow::readStrings($raw, 'labels'),
58            entityCategory: RegistryRow::readString($raw, 'entity_category'),
59            icon: RegistryRow::readString($raw, 'icon'),
60            aliases: RegistryRow::readAliasPhrases($raw),
61        );
62    }
63
64    /** @return array<string, string|list<string|null>|null> */
65    public function toArray(): array
66    {
67        return [
68            'entity_id' => $this->entityId->value,
69            'disabled_by' => $this->disabledBy,
70            'hidden_by' => $this->hiddenBy,
71            'name' => $this->name,
72            'area_id' => $this->areaId,
73            'device_id' => $this->deviceId,
74            'labels' => $this->labelIds,
75            'entity_category' => $this->entityCategory,
76            ...($this->icon === null ? [] : ['icon' => $this->icon]),
77            ...($this->aliases === null ? [] : ['aliases' => $this->aliases]),
78        ];
79    }
80
81    public function toRegisteredEntity(): RegisteredEntity
82    {
83        return new RegisteredEntity(
84            entityId: $this->entityId,
85            deviceId: DeviceId::tryFromString($this->deviceId ?? ''),
86            areaId: AreaId::tryFromString($this->areaId ?? ''),
87            labelIds: array_values(array_filter(array_map(LabelId::tryFromString(...), $this->labelIds))),
88            name: $this->name,
89            entityCategory: $this->entityCategory,
90            hiddenBy: $this->hiddenBy,
91            disabledBy: $this->disabledBy,
92            icon: $this->icon,
93            aliases: $this->aliases === null ? null : array_map(static fn(?string $phrase): EntityAlias => new EntityAlias($phrase), $this->aliases),
94        );
95    }
96}