1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Everlution\Navigation\Factory\Build\Hydrator; |
6
|
|
|
|
7
|
|
|
use Everlution\Navigation\Factory\Build\ClassDoesNotExistException; |
8
|
|
|
use Everlution\Navigation\Factory\Build\Config; |
9
|
|
|
use Everlution\Navigation\Factory\Build\ItemConfig; |
10
|
|
|
use Everlution\Navigation\Factory\ItemFactory; |
11
|
|
|
use Everlution\Navigation\Item as ItemInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class NavigationItem. |
15
|
|
|
* @author Ivan Barlog <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
abstract class Item extends Config implements ItemConfig |
18
|
|
|
{ |
19
|
|
|
const OPTION_LABEL = 'label'; |
20
|
|
|
const OPTION_CHILDREN = 'children'; |
21
|
|
|
const OPTION_MATCHES = 'matches'; |
22
|
|
|
|
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
|
27
|
|
|
$this->resolver->setRequired( |
28
|
|
|
[ |
29
|
|
|
self::OPTION_LABEL, |
30
|
|
|
self::OPTION_CHILDREN, |
31
|
|
|
self::OPTION_MATCHES, |
32
|
|
|
] |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$this->resolver->setAllowedTypes(self::OPTION_LABEL, 'string'); |
36
|
|
|
$this->resolver->setAllowedTypes(self::OPTION_CHILDREN, 'array'); |
37
|
|
|
$this->resolver->setAllowedTypes(self::OPTION_MATCHES, 'array'); |
38
|
|
|
|
39
|
|
|
$this->resolver->setDefault(self::OPTION_CHILDREN, []); |
40
|
|
|
$this->resolver->setDefault(self::OPTION_MATCHES, []); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ItemInterface $item |
46
|
|
|
* @param ItemFactory $factory |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function toArray(ItemInterface $item, ItemFactory $factory): array |
50
|
|
|
{ |
51
|
|
|
$this->checkIfSupport(get_class($item)); |
52
|
|
|
|
53
|
|
|
$result = $this->getArray($item); |
54
|
|
|
|
55
|
|
|
foreach ($item->getChildren() as $child) { |
56
|
|
|
$result[static::OPTION_CHILDREN][] = $factory->flatten($child); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $config |
64
|
|
|
* @param ItemFactory $factory |
65
|
|
|
* @return ItemInterface |
66
|
|
|
* @throws ClassDoesNotExistException |
67
|
|
|
*/ |
68
|
|
|
public function toObject(array &$config, ItemFactory $factory): ItemInterface |
69
|
|
|
{ |
70
|
|
|
$className = $this->popClassName($config); |
71
|
|
|
$this->checkIfSupport($className); |
72
|
|
|
|
73
|
|
|
$config = $this->resolve($config); |
74
|
|
|
|
75
|
|
|
if (false === class_exists($className)) { |
76
|
|
|
throw new ClassDoesNotExistException($className); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$object = $this->getObject($className, $config); |
80
|
|
|
|
81
|
|
|
foreach ($config[self::OPTION_CHILDREN] as $item) { |
82
|
|
|
$child = $factory->create($item); |
83
|
|
|
$object->addChild($child); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $object; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|