Passed
Push — master ( 42a16a...44846f )
by Andrea Marco
12:16 queued 14s
created

Meta::names()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\Enum\Attributes;
6
7
use Attribute;
8
use InvalidArgumentException;
9
10
/**
11
 * The attribute to declare the meta of an enum case.
12
 */
13
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_CLASS_CONSTANT | Attribute::IS_REPEATABLE)]
14
class Meta
15
{
16
    /**
17
     * The declared meta and related values.
18
     *
19
     * @var array<string, mixed>
20
     */
21
    protected array $all;
22
23
    /**
24
     * Instantiate the class.
25
     */
26 43
    public function __construct(mixed ...$meta)
27
    {
28 43
        foreach ($meta as $key => $value) {
29 43
            if (! is_string($key)) {
30 1
                throw new InvalidArgumentException('The name of meta must be a string');
31
            }
32
33 42
            $this->all[$key] = $value;
34
        }
35
    }
36
37
    /**
38
     * Retrieve the meta names.
39
     *
40
     * @return string[]
41
     */
42 2
    public function names(): array
43
    {
44 2
        return array_keys($this->all);
45
    }
46
47
    /**
48
     * Determine whether the given meta exists.
49
     */
50 40
    public function has(string $meta): bool
51
    {
52 40
        return array_key_exists($meta, $this->all);
53
    }
54
55
    /**
56
     * Retrieve the value for the given meta.
57
     */
58 36
    public function get(string $meta): mixed
59
    {
60 36
        return $this->all[$meta] ?? null;
61
    }
62
}
63