Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
18 / 27
50.00% covered (danger)
50.00%
8 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
MenuItem
66.67% covered (warning)
66.67%
18 / 27
50.00% covered (danger)
50.00%
8 / 16
34.81
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 rewind
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 current
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 key
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 next
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 valid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 type
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 title
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 image
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 class
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 level
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 children
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 setChildren
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasChildren
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 translated
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
3.21
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Finder;
6
7use Cosray\Context;
8use Generator;
9use Iterator;
10
11class MenuItem implements Iterator
12{
13    protected readonly array $data;
14    protected array $children;
15
16    public function __construct(
17        protected readonly Context $context,
18        protected readonly array $item,
19    ) {
20        $this->data = json_decode($item['data'], true);
21        $this->children = $item['children'];
22    }
23
24    public function rewind(): void
25    {
26        reset($this->children);
27    }
28
29    public function current(): MenuItem
30    {
31        return new MenuItem($this->context, current($this->children));
32    }
33
34    public function key(): string
35    {
36        return key($this->children);
37    }
38
39    public function next(): void
40    {
41        next($this->children);
42    }
43
44    public function valid(): bool
45    {
46        return key($this->children) !== null;
47    }
48
49    public function type(): string
50    {
51        return $this->data['type'];
52    }
53
54    public function title(): string
55    {
56        return $this->translated('title');
57    }
58
59    public function path(): string
60    {
61        return $this->translated('path');
62    }
63
64    public function image(): ?string
65    {
66        $image = $this->data['image'] ?? null;
67
68        if (!$image) {
69            return null;
70        }
71
72        return sprintf('/assets/menu/%s/%s', $this->item['menu'], $image);
73    }
74
75    public function class(): ?string
76    {
77        return $this->data['class'] ?? null;
78    }
79
80    public function level(): int
81    {
82        return $this->item['level'];
83    }
84
85    public function children(): Generator
86    {
87        foreach ($this->children as $child) {
88            yield new MenuItem($this->context, $child);
89        }
90    }
91
92    public function setChildren(array $children): void
93    {
94        $this->children = $children;
95    }
96
97    public function hasChildren(): bool
98    {
99        return count($this->children) > 0;
100    }
101
102    protected function translated(string $key): string
103    {
104        $locale = $this->context->locale();
105
106        while ($locale) {
107            $value = $this->data[$key][$locale->id] ?? null;
108
109            if ($value) {
110                return $value;
111            }
112
113            $locale = $locale->fallback();
114        }
115
116        return '';
117    }
118}