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