forked from KnpLabs/KnpMenuBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuItem.php
1300 lines (1129 loc) · 32.8 KB
/
MenuItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Knplabs\Bundle\MenuBundle;
use Knplabs\Bundle\MenuBundle\Renderer\RendererInterface;
use Knplabs\Bundle\MenuBundle\Renderer\ListRenderer;
/**
* This is your base menu item. It roughly represents a single <li> tag
* and is what you should interact with most of the time by default.
* Decoupled from Symfony2, can be used in any PHP 5.3 project.
* Originally taken from ioMenuPlugin (http://github.com/weaverryan/ioMenuPlugin)
*/
class MenuItem implements \ArrayAccess, \Countable, \IteratorAggregate
{
/**
* Properties on this menu item
*/
protected
$name = null, // the name of this menu item (used for id by parent menu)
$label = null, // the label to output, name is used by default
$linkAttributes = array(), // an array of attributes for the item link
$labelAttributes = array(), // an array of attributes for the item text
$uri = null, // the uri to use in the anchor tag
$attributes = array(); // an array of attributes for the li
/**
* Options related to rendering
*/
protected
$show = true, // boolean to render this menu
$showChildren = true; // boolean to render the children of this menu
/**
* Metadata on this menu item
*/
protected
$children = array(), // an array of MenuItem children
$num = null, // the order number this menu is in its parent
$parent = null, // parent MenuItem
$isCurrent = null, // whether or not this menu item is current
$currentUri = null, // the current uri to use for selecting current menu
$currentAsLink = true; // boolean to render the current uri as a link or not
/**
* The renderer used to render this menu
* @var RendererInterface
*/
protected $renderer = null;
/**
* Class constructor
*
* @param string $name The name of this menu, which is how its parent will
* reference it. Also used as label if label not specified
* @param string $uri The uri for this menu to use. If not specified,
* text will be shown without a link
* @param array $attributes Attributes to place on the li tag of this menu item
*/
public function __construct($name, $uri = null, $attributes = array())
{
$this->name = (string) $name;
$this->uri = $uri;
$this->attributes = $attributes;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
* @return MenuItem
*/
public function setName($name)
{
if ($this->name == $name) {
return $this;
}
if ($this->getParent() && $this->getParent()->getChild($name)) {
throw new \InvalidArgumentException('Cannot rename item, name is already used by sibling.');
}
$oldName = $this->name;
$this->name = $name;
if ($this->getParent()) {
$this->getParent()->updateChildId($this, $oldName);
}
return $this;
}
/**
* Updates id for child based on new name.
*
* Used internally after renaming item which has parent.
*
* @param MenuItem $child Item whose name has been changed.
* @param string $oldName Old (previous) name of item.
*
*/
protected function updateChildId(MenuItem $child, $oldName)
{
$names = array_keys($this->getChildren());
$items = array_values($this->getChildren());
$offset = array_search($oldName, $names);
$names[$offset] = $child->getName();
$children = array_combine($names, $items);
$this->setChildren($children);
}
/**
* Get the uri for a menu item
*
* @return string
*/
public function getUri()
{
return $this->uri;
}
/**
* Set the uri for a menu item
*
* @param string $uri The uri to set on this menu item
* @return MenuItem
*/
public function setUri($uri)
{
$this->uri = $uri;
return $this;
}
/**
* Returns the label that will be used to render this menu item
*
* Defaults to the name of no label was specified
*
* @return string
*/
public function getLabel()
{
return ($this->label !== null) ? $this->label : $this->name;
}
/**
* @param string $label The text to use when rendering this menu item
* @return MenuItem
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}
/**
* @param array $attributes
* @return MenuItem
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
return $this;
}
/**
* @param string $name The name of the attribute to return
* @param mixed $default The value to return if the attribute doesn't exist
*
* @return mixed
*/
public function getAttribute($name, $default = null)
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return $default;
}
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;
return $this;
}
/**
* @return array
*/
public function getLinkAttributes()
{
return $this->linkAttributes;
}
/**
* @param array $linkAttributes
* @return MenuItem
*/
public function setLinkAttributes(array $linkAttributes)
{
$this->linkAttributes = $linkAttributes;
return $this;
}
/**
* @param string $name The name of the attribute to return
* @param mixed $default The value to return if the attribute doesn't exist
*
* @return mixed
*/
public function getLinkAttribute($name, $default = null)
{
if (isset($this->linkAttributes[$name])) {
return $this->linkAttributes[$name];
}
return $default;
}
/**
* @param string $name
* @param string $value
*
* @return MenuItem
*/
public function setLinkAttribute($name, $value)
{
$this->linkAttributes[$name] = $value;
return $this;
}
/**
* @return array
*/
public function getLabelAttributes()
{
return $this->labelAttributes;
}
/**
* @param array $labelAttributes
* @return MenuItem
*/
public function setLabelAttributes(array $labelAttributes)
{
$this->labelAttributes = $labelAttributes;
return $this;
}
/**
* @param string $name The name of the attribute to return
* @param mixed $default The value to return if the attribute doesn't exist
*
* @return mixed
*/
public function getLabelAttribute($name, $default = null)
{
if (isset($this->labelAttributes[$name])) {
return $this->labelAttributes[$name];
}
return $default;
}
public function setLabelAttribute($name, $value)
{
$this->labelAttributes[$name] = $value;
return $this;
}
/**
* @return bool Whether or not this menu item should show its children.
*/
public function getShowChildren()
{
return $this->showChildren;
}
/**
* Set whether or not this menu item should show its children
*
* @param bool $bool
* @return MenuItem
*/
public function setShowChildren($bool)
{
$this->showChildren = (bool) $bool;
return $this;
}
/**
* @return bool Whether or not to show this menu item
*/
public function getShow()
{
return $this->show;
}
/**
* Set whether or not this menu to show this menu item
*
* @param bool $bool
* @return MenuItem
*/
public function setShow($bool)
{
$this->show = (bool) $bool;
return $this;
}
/**
* Whether or not this menu item should be rendered or not based on all the available factors
*
* @return boolean
*/
public function shouldBeRendered()
{
return $this->getShow();
}
/**
* Add a child menu item to this menu
*
* @param mixed $child An MenuItem object or the name of a new menu to create
* @param string $uri If creating a new menu, the uri for that menu
* @param string $attributes If creating a new menu, the attributes for that menu
* @param string $class The class for menu item, if it needs to be created
*
* @return MenuItem The child menu item
*/
public function addChild($child, $uri = null, $attributes = array(), $class = null)
{
if (!$child instanceof MenuItem) {
$child = $this->createChild($child, $uri, $attributes, $class);
}
elseif ($child->getParent()) {
throw new \InvalidArgumentException('Cannot add menu item as child, it already belongs to another menu (e.g. has a parent).');
}
$child->setParent($this);
$child->setShowChildren($this->getShowChildren());
$child->setCurrentUri($this->getCurrentUri());
$child->setNum($this->count());
$this->children[$child->getName()] = $child;
return $child;
}
/**
* Returns the child menu identified by the given name
*
* @param string $name Then name of the child menu to return
* @return MenuItem|null
*/
public function getChild($name)
{
return isset($this->children[$name]) ? $this->children[$name] : null;
}
/**
* Moves child to specified position. Rearange other children accordingly.
*
* @param numeric $position Position to move child to.
*
*/
public function moveToPosition($position)
{
$this->getParent()->moveChildToPosition($this, $position);
}
/**
* Moves child to specified position. Rearange other children accordingly.
*
* @param MenuItem $child Child to move.
* @param numeric $position Position to move child to.
*/
public function moveChildToPosition(MenuItem $child, $position)
{
$name = $child->getName();
$order = array_keys($this->children);
$oldPosition = array_search($name, $order);
unset($order[$oldPosition]);
$order = array_values($order);
array_splice($order, $position, 0, $name);
$this->reorderChildren($order);
}
/**
* Moves child to first position. Rearange other children accordingly.
*/
public function moveToFirstPosition()
{
$this->moveToPosition(0);
}
/**
* Moves child to last position. Rearange other children accordingly.
*/
public function moveToLastPosition()
{
$this->moveToPosition($this->getParent()->count());
}
/**
* Reorder children.
*
* @param array $order New order of children.
*/
public function reorderChildren($order)
{
if (count($order) != $this->count()) {
throw new \InvalidArgumentException('Cannot reorder children, order does not contain all children.');
}
$newChildren = array();
foreach($order as $name) {
if (!isset($this->children[$name])) {
throw new \InvalidArgumentException('Cannot find children named '.$name);
}
$child = $this->children[$name];
$newChildren[$name] = $child;
}
$this->children = $newChildren;
$this->resetChildrenNum();
}
/**
* Makes a deep copy of menu tree. Every item is copied as another object.
*
* @return MenuItem
*/
public function copy()
{
$newMenu = clone $this;
$newMenu->children = array();
$newMenu->setParent(null);
foreach($this->getChildren() as $child) {
$newMenu->addChild($child->copy());
}
return $newMenu;
}
/**
* Get slice of menu as another menu.
*
* If offset and/or length are numeric, it works like in array_slice function:
*
* If offset is non-negative, slice will start at the offset.
* If offset is negative, slice will start that far from the end.
*
* If length is zero, slice will have all elements.
* If length is positive, slice will have that many elements.
* If length is negative, slice will stop that far from the end.
*
* It's possible to mix names/object/numeric, for example:
* slice("child1", 2);
* slice(3, $child5);
*
* @param mixed $offset Name of child, child object, or numeric offset.
* @param mixed $length Name of child, child object, or numeric length.
* @return MenuItem Slice of menu.
*/
public function slice($offset, $length = 0)
{
$count = $this->count();
$names = array_keys($this->getChildren());
if (is_numeric($offset)) {
$offset = ($offset >= 0) ? $offset : $count + $offset;
$from = (isset($names[$offset])) ? $names[$offset] : "";
}
else {
$child = ($offset instanceof MenuItem) ? $offset : $this->getChild($offset);
$offset = ($child) ? $child->getNum() : 0;
$from = ($child) ? $child->getName() : "";
}
if (is_numeric($length)) {
if ($length == 0) {
$offset2 = $count - 1;
}
else {
$offset2 = ($length > 0) ? $offset + $length - 1 : $count - 1 + $length;
}
$to = (isset($names[$offset2])) ? $names[$offset2] : "";
}
else {
$to = ($length instanceof MenuItem) ? $length->getName() : $length;
}
return $this->sliceFromTo($from, $to);
}
/**
* Get slice of menu as another menu.
*
* Internal method.
*
* @param string $offset Name of child.
* @param string $length Name of child.
* @return MenuItem
*/
private function sliceFromTo($from, $to)
{
$newMenu = $this->copy();
$newChildren = array();
$copy = false;
foreach($newMenu->getChildren() as $child) {
if ($child->getName() == $from) {
$copy = true;
}
if ($copy == true) {
$newChildren[$child->getName()] = $child;
}
if ($child->getName() == $to) {
break;
}
}
$newMenu->setChildren($newChildren);
$newMenu->resetChildrenNum();
return $newMenu;
}
/**
* Split menu into two distinct menus.
*
* @param mixed $length Name of child, child object, or numeric length.
* @return array Array with two menus, with "primary" and "secondary" key
*/
public function split($length)
{
$count = $this->count();
if (!is_numeric ($length)) {
if (!($length instanceof MenuItem)) {
$length = $this->getChild($length);
}
$length = ($length != null) ? $length->getNum() + 1 : $count;
}
$ret = array();
$ret['primary'] = $this->slice(0, $length);
$ret['secondary'] = $this->slice($length);
return $ret;
}
/**
* Returns the level of this menu item
*
* The root menu item is 0, followed by 1, 2, etc
*
* @return integer
*/
public function getLevel()
{
return $this->parent ? $this->parent->getLevel() + 1 : 0;
}
/**
* Returns the root MenuItem of this menu tree
*
* @return MenuItem
*/
public function getRoot()
{
$obj = $this;
do {
$found = $obj;
}
while ($obj = $obj->getParent());
return $found;
}
/**
* Returns whether or not this menu item is the root menu item
*
* @return bool
*/
public function isRoot()
{
return (bool) !$this->getParent();
}
/**
* @return MenuItem|null
*/
public function getParent()
{
return $this->parent;
}
/**
* Used internally when adding and removing children
*
* @param MenuItem $parent
* @return MenuItem
*/
public function setParent(MenuItem $parent = null)
{
return $this->parent = $parent;
}
/**
* @return array of MenuItem objects
*/
public function getChildren()
{
return $this->children;
}
/**
* @param array $children An array of MenuItem objects
* @return MenuItem
*/
public function setChildren(array $children)
{
$this->children = $children;
return $this;
}
/**
* Returns the index that this child is within its parent.
*
* Primarily used internally to calculate first and last
*
* @return integer
*/
public function getNum()
{
return $this->num;
}
/**
* Sets the index that this child is within its parent.
*
* Primarily used internally to calculate first and last
*
* @return void
*/
public function setNum($num)
{
$this->num = $num;
}
/**
* Reset children nums.
*
* Primarily called after changes to children (removing, reordering, etc)
*
* @return void
*/
protected function resetChildrenNum()
{
$i = 0;
foreach ($this->children as $child) {
$child->setNum($i++);
}
}
/**
* Creates a new MenuItem to be the child of this menu
*
* @param string $name
* @param string $uri
* @param array $attributes
*
* @return MenuItem
*/
protected function createChild($name, $uri = null, $attributes = array(), $class = null)
{
if ($class === null) {
$class = get_class($this);
}
return new $class($name, $uri, $attributes);
}
/**
* Removes a child from this menu item
*
* @param mixed $name The name of MenuItem instance to remove
*/
public function removeChild($name)
{
$name = ($name instanceof MenuItem) ? $name->getName() : $name;
if (isset($this->children[$name])) {
// unset the child and reset it so it looks independent
$this->children[$name]->setParent(null);
$this->children[$name]->setNum(null);
unset($this->children[$name]);
$this->resetChildrenNum();
}
}
/**
* @return MenuItem
*/
public function getFirstChild()
{
return reset($this->children);
}
/**
* @return MenuItem
*/
public function getLastChild()
{
return end($this->children);
}
/**
* Returns whether or not this menu items has viewable children
*
* This menu MAY have children, but this will return false if the current
* user does not have access to vew any of those items
*
* @return boolean;
*/
public function hasChildren()
{
foreach ($this->children as $child) {
if ($child->shouldBeRendered()) {
return true;
}
}
return false;
}
/**
* Renders the menu tree by using the statically set renderer.
*
* Depth values corresppond to:
* * 0 - no children displayed at all (would return a blank string)
* * 1 - directly children only
* * 2 - children and grandchildren
*
* @param integer $depth The depth of children to render
*
* @return string
*/
public function render($depth = null)
{
return $this->getRenderer()->render($this, $depth);
}
/**
* Sets renderer which will be used to render menu items.
*
* @param RendererInterface $renderer Renderer.
*/
public function setRenderer(RendererInterface $renderer)
{
$this->renderer = $renderer;
}
/**
* Gets renderer which is used to render menu items.
*
* @return RendererInterface $renderer Renderer.
*/
public function getRenderer()
{
if(null === $this->renderer) {
if($this->isRoot()) {
$this->setRenderer(new ListRenderer());
}
else {
return $this->getParent()->getRenderer();
}
}
return $this->renderer;
}
/**
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* Renders the anchor tag for this menu item.
*
* If no uri is specified, or if the uri fails to generate, the
* label will be output.
*
* @return string
*/
public function renderLink()
{
$label = $this->renderLabel();
$uri = $this->getUri();
if (!$uri) {
return $label;
}
return sprintf('<a href="%s">%s</a>', $uri, $label);
}
/**
* Renders the label of this menu
*
* @return string
*/
public function renderLabel()
{
return $this->getLabel();
}
/**
* A string representation of this menu item
*
* e.g. Top Level > Second Level > This menu
*
* @param string $separator
* @return string
*/
public function getPathAsString($separator = ' > ')
{
$children = array();
$obj = $this;
do {
$children[] = $obj->renderLabel();
}
while ($obj = $obj->getParent());
return implode($separator, array_reverse($children));
}
/**
* Renders an array of label => uri pairs ready to be used for breadcrumbs.
*
* The subItem can be one of the following forms
* * 'subItem'
* * array('subItem' => '@homepage')
* * array('subItem1', 'subItem2')
*
* @example
* // drill down to the Documentation menu item, then add "Chapter 1" to the breadcrumb
* $arr = $menu['Documentation']->getBreadcrumbsArray('Chapter 1');
* foreach ($arr as $name => $url)
* {
*
* }
*
* @param mixed $subItem A string or array to append onto the end of the array
* @return array
*/
public function getBreadcrumbsArray($subItem = null)
{
$breadcrumbs = array();
$obj = $this;
if ($subItem) {
if (!is_array($subItem)) {
$subItem = array((string) $subItem => null);
}
$subItem = array_reverse($subItem);
foreach ($subItem as $key => $value) {
if (is_numeric($key)) {
$key = $value;
$value = null;
}
$breadcrumbs[(string) $key] = $value;
}
}
do {
$label = $obj->renderLabel();
$breadcrumbs[$label] = $obj->getUri();
}
while ($obj = $obj->getParent());
return array_reverse($breadcrumbs);
}
/**
* Returns the current menu item if it is a child of this menu item
*
* @return bool|MenuItem
*/
public function getCurrent()
{
if ($this->getIsCurrent()) {
return $this;
}
foreach ($this->children as $child) {
if ($current = $child->getCurrent()) {
return $current;
}
}
return false;
}
/**
* Set whether or not this menu item is "current"
*
* @param boolean $bool Specify that this menu item is current
* @return boolean
*/
public function setIsCurrent($bool)
{
$this->isCurrent = (bool) $bool;
return $this;
}
/**
* Get whether or not this menu item is "current"
*
* @return bool
*/
public function getIsCurrent()
{
if (null === $this->isCurrent) {
$currentUri = $this->getCurrentUri();
$this->isCurrent = null !== $currentUri && ($this->getUri() === $currentUri);
}
return $this->isCurrent;
}
/**
* Returns whether or not this menu is an ancestor of the current menu item
*
* @return boolean
*/
public function getIsCurrentAncestor()
{
foreach ($this->getChildren() as $child) {
if ($child->getIsCurrent() || $child->getIsCurrentAncestor()) {
return true;
}
}
return false;
}
/**
* @return bool Whether or not this menu item is last in its parent
*/
public function isLast()
{
// if this is root, then return false
if ($this->isRoot()) {
return false;
}