Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
24 / 32
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Meta
75.00% covered (warning)
75.00%
24 / 32
66.67% covered (warning)
66.67%
4 / 6
20.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 __get
100.00% covered (success)
100.00%
5 / 5
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
 has
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 get
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
6.40
 all
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Node;
6
7use Cosray\Exception\NoSuchProperty;
8use Cosray\Fulltext\Search;
9
10class Meta
11{
12    private readonly object $node;
13    public readonly string $uid;
14    public readonly Type $type;
15    public readonly ?Search $search;
16
17    public function __construct(
18        object $node,
19        private readonly Types $types,
20    ) {
21        $this->node = Wrapper::unwrap($node);
22        $this->uid = (string) (Factory::meta($this->node, 'uid') ?? '');
23        $this->type = $this->types->typeOf($this->node::class);
24        $search = Factory::meta($this->node, 'search');
25        $this->search = $search instanceof Search ? $search : null;
26    }
27
28    public function __get(string $name): mixed
29    {
30        if (!$this->has($name)) {
31            throw new NoSuchProperty(
32                "The node '" . $this->node::class . "' doesn't have the meta property '{$name}'",
33            );
34        }
35
36        return $this->get($name);
37    }
38
39    public function __isset(string $name): bool
40    {
41        return $this->has($name) && $this->get($name) !== null;
42    }
43
44    public function has(string $name): bool
45    {
46        if (in_array($name, ['name', 'class', 'classname'], true)) {
47            return true;
48        }
49
50        $data = Factory::dataFor($this->node);
51
52        if (array_key_exists($name, $data)) {
53            return true;
54        }
55
56        return $this->type->has($name);
57    }
58
59    public function get(string $key, mixed $default = null): mixed
60    {
61        $data = Factory::dataFor($this->node);
62
63        if (array_key_exists($key, $data)) {
64            return $data[$key];
65        }
66
67        return match ($key) {
68            'name' => $this->type->label,
69            'class' => $this->node::class,
70            'classname' => basename(str_replace('\\', '/', $this->node::class)),
71            default => $this->type->get($key, $default),
72        };
73    }
74
75    /**
76     * @return array<string, mixed>
77     */
78    public function all(): array
79    {
80        return array_merge($this->type->all(), Factory::dataFor($this->node), [
81            'uid' => $this->uid,
82            'name' => $this->type->label,
83            'class' => $this->node::class,
84            'classname' => basename(str_replace('\\', '/', $this->node::class)),
85        ]);
86    }
87}