1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CMS Kernel package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-present LIN3S <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LIN3S\CMSKernel\Domain\Model\Menu; |
13
|
|
|
|
14
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\Locale; |
15
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\Translation; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Beñat Espiña <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class MenuTranslation extends Translation |
21
|
|
|
{ |
22
|
|
|
private $id; |
23
|
|
|
private $name; |
24
|
|
|
private $tree; |
25
|
|
|
|
26
|
|
|
public function __construct(MenuTranslationId $id, Locale $locale, MenuName $name) |
27
|
|
|
{ |
28
|
|
|
parent::__construct($locale); |
29
|
|
|
$this->id = $id; |
30
|
|
|
$this->name = $name; |
31
|
|
|
$this->tree = new MenuItemCollection(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function addItem( |
35
|
|
|
MenuItemLink $link, |
36
|
|
|
MenuItemOrder $order, |
37
|
|
|
MenuItemId $parentId = null, |
38
|
|
|
MenuItemId $menuItemId = null |
39
|
|
|
) { |
40
|
|
|
$menuItemId = null === $menuItemId ? MenuItemId::generate() : $menuItemId; |
41
|
|
|
|
42
|
|
|
$menuItem = new MenuItem($menuItemId, $link, $order, $parentId); |
43
|
|
|
$menuItemReflection = new \ReflectionClass($menuItem); |
44
|
|
|
$menuTranslation = $menuItemReflection->getProperty('menuTranslation'); |
45
|
|
|
$menuTranslation->setAccessible(true); |
46
|
|
|
$menuTranslation->setValue($menuItem, $this); |
47
|
|
|
|
48
|
|
|
$this->tree->add($menuItem); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function removeItem(MenuItemId $id) |
52
|
|
|
{ |
53
|
|
|
$this->tree->removeById($id); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function id() |
57
|
|
|
{ |
58
|
|
|
return $this->id; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function name() |
62
|
|
|
{ |
63
|
|
|
return $this->name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function tree() |
67
|
|
|
{ |
68
|
|
|
return $this->tree; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function item(MenuItemId $id) |
72
|
|
|
{ |
73
|
|
|
$menuItem = null; |
74
|
|
|
foreach ($this->tree() as $item) { |
75
|
|
|
if ($id->equals($item->id())) { |
76
|
|
|
$menuItem = $item; |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
if (!$menuItem instanceof MenuItem) { |
81
|
|
|
throw new MenuItemDoesNotExistException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $menuItem; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function __toString() |
88
|
|
|
{ |
89
|
|
|
return (string) $this->id->id(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|