Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Icon
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 parseArgs
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
7
 isStringMap
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Schema;
6
7use Attribute;
8use ValueError;
9
10#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY)]
11readonly class Icon
12{
13    /** @var array<string, mixed> */
14    public array $args;
15
16    public function __construct(
17        public string $id,
18        mixed ...$args,
19    ) {
20        $this->args = $this->parseArgs($args);
21    }
22
23    /**
24     * @param array<array-key, mixed> $args
25     * @return array<string, mixed>
26     */
27    private function parseArgs(array $args): array
28    {
29        if (count($args) === 0) {
30            return [];
31        }
32
33        if (count($args) === 1 && array_key_exists(0, $args) && is_array($args[0])) {
34            if ($this->isStringMap($args[0])) {
35                return $args[0];
36            }
37
38            throw new ValueError('Icon arguments must be an associative array or named arguments');
39        }
40
41        if ($this->isStringMap($args)) {
42            return $args;
43        }
44
45        throw new ValueError('Icon arguments must be an associative array or named arguments');
46    }
47
48    /** @param array<array-key, mixed> $value */
49    private function isStringMap(array $value): bool
50    {
51        foreach ($value as $key => $_) {
52            if (!is_string($key)) {
53                return false;
54            }
55        }
56
57        return true;
58    }
59}