1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\AdminBundle\Admin\Routing; |
13
|
|
|
|
14
|
|
|
class ListRouteBuilder implements ListRouteBuilderInterface |
15
|
|
|
{ |
16
|
|
|
const VIEW = 'sulu_admin.list'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Route |
20
|
|
|
*/ |
21
|
|
|
private $route; |
22
|
|
|
|
23
|
|
|
public function __construct(string $name, string $path) |
24
|
|
|
{ |
25
|
|
|
$this->route = new Route($name, $path, static::VIEW); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setResourceKey(string $resourceKey): ListRouteBuilderInterface |
29
|
|
|
{ |
30
|
|
|
$this->route->setOption('resourceKey', $resourceKey); |
31
|
|
|
|
32
|
|
|
return $this; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setListkey(string $listKey): ListRouteBuilderInterface |
36
|
|
|
{ |
37
|
|
|
$this->route->setOption('listKey', $listKey); |
38
|
|
|
|
39
|
|
|
return $this; |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setTitle(string $title): ListRouteBuilderInterface |
43
|
|
|
{ |
44
|
|
|
$this->route->setOption('title', $title); |
45
|
|
|
|
46
|
|
|
return $this; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setTabTitle(string $tabTitle): ListRouteBuilderInterface |
50
|
|
|
{ |
51
|
|
|
$this->route->setOption('tabTitle', $tabTitle); |
52
|
|
|
|
53
|
|
|
return $this; |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setTabOrder(int $tabOrder): ListRouteBuilderInterface |
57
|
|
|
{ |
58
|
|
|
$this->route->setOption('tabOrder', $tabOrder); |
59
|
|
|
|
60
|
|
|
return $this; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setTabCondition(string $tabCondition): ListRouteBuilderInterface |
64
|
|
|
{ |
65
|
|
|
$this->route->setOption('tabCondition', $tabCondition); |
66
|
|
|
|
67
|
|
|
return $this; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function addListAdapters(array $listAdapters): ListRouteBuilderInterface |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$oldListAdapters = $this->route->getOption('adapters'); |
73
|
|
|
$newListAdapters = $oldListAdapters ? array_merge($oldListAdapters, $listAdapters) : $listAdapters; |
74
|
|
|
$this->route->setOption('adapters', $newListAdapters); |
75
|
|
|
|
76
|
|
|
return $this; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function addLocales(array $locales): ListRouteBuilderInterface |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$oldLocales = $this->route->getOption('locales'); |
82
|
|
|
$newLocales = $oldLocales ? array_merge($oldLocales, $locales) : $locales; |
83
|
|
|
$this->route->setOption('locales', $newLocales); |
84
|
|
|
|
85
|
|
|
return $this; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setDefaultLocale(string $locale): ListRouteBuilderInterface |
89
|
|
|
{ |
90
|
|
|
$this->route->setAttributeDefault('locale', $locale); |
91
|
|
|
|
92
|
|
|
return $this; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function addToolbarActions(array $toolbarActions): ListRouteBuilderInterface |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$oldToolbarActions = $this->route->getOption('toolbarActions'); |
98
|
|
|
$newToolbarActions = $oldToolbarActions ? array_merge($oldToolbarActions, $toolbarActions) : $toolbarActions; |
99
|
|
|
$this->route->setOption('toolbarActions', $newToolbarActions); |
100
|
|
|
|
101
|
|
|
return $this; |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setAddRoute(string $addRoute): ListRouteBuilderInterface |
105
|
|
|
{ |
106
|
|
|
$this->route->setOption('addRoute', $addRoute); |
107
|
|
|
|
108
|
|
|
return $this; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function setEditRoute(string $editRoute): ListRouteBuilderInterface |
112
|
|
|
{ |
113
|
|
|
$this->route->setOption('editRoute', $editRoute); |
114
|
|
|
|
115
|
|
|
return $this; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setBackRoute(string $backRoute): ListRouteBuilderInterface |
119
|
|
|
{ |
120
|
|
|
$this->route->setOption('backRoute', $backRoute); |
121
|
|
|
|
122
|
|
|
return $this; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function enableSearching(): ListRouteBuilderInterface |
126
|
|
|
{ |
127
|
|
|
$this->route->setOption('searchable', true); |
128
|
|
|
|
129
|
|
|
return $this; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function disableSearching(): ListRouteBuilderInterface |
133
|
|
|
{ |
134
|
|
|
$this->route->setOption('searchable', false); |
135
|
|
|
|
136
|
|
|
return $this; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function enableMoving(): ListRouteBuilderInterface |
140
|
|
|
{ |
141
|
|
|
$this->route->setOption('movable', true); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function disableMoving(): ListRouteBuilderInterface |
147
|
|
|
{ |
148
|
|
|
$this->route->setOption('movable', false); |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
View Code Duplication |
public function addRouterAttributesToListStore(array $routerAttributesToListStore): ListRouteBuilderInterface |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$oldRouterAttributesToListStore = $this->route->getOption('routerAttributesToListStore'); |
156
|
|
|
$newRouterAttributesToListStore = $oldRouterAttributesToListStore ? array_merge($oldRouterAttributesToListStore, $routerAttributesToListStore) : $routerAttributesToListStore; |
157
|
|
|
$this->route->setOption('routerAttributesToListStore', $newRouterAttributesToListStore); |
158
|
|
|
|
159
|
|
|
return $this; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function setParent(string $parent): ListRouteBuilderInterface |
163
|
|
|
{ |
164
|
|
|
$this->route->setParent($parent); |
165
|
|
|
|
166
|
|
|
return $this; |
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getRoute(): Route |
170
|
|
|
{ |
171
|
|
|
if (!$this->route->getOption('resourceKey')) { |
172
|
|
|
throw new \DomainException( |
173
|
|
|
'A route for a list view needs a "resourceKey" option.' |
174
|
|
|
. ' You have likely forgotten to call the "setResourceKey" method.' |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if (!$this->route->getOption('listKey')) { |
179
|
|
|
throw new \DomainException( |
180
|
|
|
'A route for a list view needs a "listKey" option.' |
181
|
|
|
. ' You have likely forgotten to call the "setListKey" method.' |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (!$this->route->getOption('adapters')) { |
186
|
|
|
throw new \DomainException( |
187
|
|
|
'A route for a list needs a "adapters" option.' |
188
|
|
|
. ' You have likely forgotten to call the "addListAdapters" method.' |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ($this->route->getOption('locales') && false === strpos($this->route->getPath(), ':locale')) { |
193
|
|
|
throw new \DomainException( |
194
|
|
|
'A route for a list needs a ":locale" placeholder in its URL if some "locales" have been set.' |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (!$this->route->getOption('locales') && false !== strpos($this->route->getPath(), ':locale')) { |
199
|
|
|
throw new \DomainException( |
200
|
|
|
'A route for a list cannot have a ":locale" placeholder in its URL if no "locales" have been set.' |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return clone $this->route; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.