Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.00% covered (warning)
85.00%
17 / 20
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Type
85.00% covered (warning)
85.00%
17 / 20
66.67% covered (warning)
66.67%
4 / 6
13.57
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 __isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
5.02
 has
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 all
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Node;
6
7use Cosray\Exception\NoSuchProperty;
8
9class Type
10{
11    public readonly string $class;
12    public readonly string $classname;
13
14    private readonly Schema $schema;
15
16    /**
17     * @param class-string $class
18     */
19    public function __construct(
20        string $class,
21        Types $types,
22    ) {
23        $this->class = $class;
24        $this->classname = basename(str_replace('\\', '/', $class));
25        $this->schema = $types->schemaOf($class);
26    }
27
28    public function __get(string $key): mixed
29    {
30        if (!$this->has($key)) {
31            throw new NoSuchProperty("The node type '{$this->class}' doesn't have the property '{$key}'");
32        }
33
34        return $this->get($key);
35    }
36
37    public function __isset(string $key): bool
38    {
39        return $this->has($key) && $this->get($key) !== null;
40    }
41
42    public function get(string $key, mixed $default = null): mixed
43    {
44        return match ($key) {
45            'class' => $this->class,
46            'classname' => $this->classname,
47            default => $this->schema->get($key, $default),
48        };
49    }
50
51    public function has(string $key): bool
52    {
53        return match ($key) {
54            'class', 'classname' => true,
55            default => $this->schema->has($key),
56        };
57    }
58
59    /**
60     * @return array<string, mixed>
61     */
62    public function all(): array
63    {
64        return array_merge($this->schema->properties(), [
65            'class' => $this->class,
66            'classname' => $this->classname,
67        ]);
68    }
69}