Issues (5)

src/Services/Resolver/BreadcrumbsResolver.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Braunstetter\MenuBundle\Services\Resolver;
6
7
use Braunstetter\MenuBundle\Contracts\MenuInterface;
8
use Braunstetter\MenuBundle\Contracts\MenuItemInterface;
9
use Webmozart\Assert\Assert;
10
11
class BreadcrumbsResolver extends AbstractMenuResolver
12
{
13
    /**
14
     * @param array<array-key, mixed> $context
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
15
     * @return MenuItemInterface[]
16
     */
17
    public function get(string $name, array $context): array
18
    {
19
        $this->setSubnavItem($context);
20
21
        $result = [];
22
23
        foreach ($this->entries as $menu) {
24
            if ($this->getHandle($menu) === $name) {
25
                $result = $this->resolve($menu, $result);
26
            }
27
        }
28
29
        return $result;
30
    }
31
32
    /**
33
     * @param MenuItemInterface[] $result
34
     * @return  array<MenuItemInterface>
35
     */
36
    private function resolve(MenuInterface $menu, array $result): array
37
    {
38
        $menuItems = $this->resolveMenu($menu);
39
        Assert::allIsInstanceOf(
40
            $menuItems,
41
            MenuItemInterface::class,
42
            'The callable must return an iterable of MenuItems'
43
        );
44
45
        foreach ($menuItems as $item) {
46
            $crumb = $this->findBreadCrumb($item);
47
            if ($crumb) {
48
                $result[] = $item;
49
                break;
50
            }
51
        }
52
53
        return $result;
54
    }
55
56
    private function findBreadCrumb(MenuItemInterface $item): MenuItemInterface|bool
57
    {
58
        if ($this->matches($item)) {
59
            return $item;
60
        }
61
62
        if ($this->oneOfTheChildrenMatches($item)) {
63
            return $item;
64
        }
65
66
        return false;
67
    }
68
}
69