OrderedContainer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\Navigation;
6
7
use Everlution\Navigation\Item\ItemInterface;
8
use Everlution\Navigation\Item\SortableInterface;
9
10
/**
11
 * Class OrderedContainer.
12
 *
13
 * @author Ivan Barlog <[email protected]>
14
 */
15
class OrderedContainer implements ContainerInterface
16
{
17
    /** @var ContainerInterface */
18
    private $container;
19
    /** @var ItemInterface[] */
20
    private $items;
21
22
    public function __construct(ContainerInterface $container)
23
    {
24
        $this->container = $container;
25
    }
26
27
    /**
28
     * @return ItemInterface[]
29
     */
30
    public function getItems(): array
31
    {
32
        if (empty($this->items)) {
33
            $this->items = $this->container->getItems();
34
        }
35
36
        uasort($this->items, function($a, $b) {
37
            if (!($a instanceof SortableInterface && $b instanceof SortableInterface)) {
38
                return 0;
39
            }
40
41
            return $a->getOrder() <=> $b->getOrder();
42
        });
43
44
        return $this->items;
45
    }
46
47
    public function get(string $identifier): ItemInterface
48
    {
49
        return $this->items[$identifier];
50
    }
51
}
52