1 | <?php |
||
36 | abstract class Menu { |
||
37 | |||
38 | private $menuItemFormatter = null; |
||
39 | private $itemListFormatter = null; |
||
40 | |||
41 | abstract public function getHtml(); |
||
42 | |||
43 | /** |
||
44 | * @param string $href |
||
45 | * @param string $text |
||
46 | * @param int $depth |
||
47 | * @param string $subitems |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 1 | protected function getHtmlForMenuItem( $href, $text, $depth, $subitems ) { |
|
52 | 1 | return call_user_func( $this->getMenuItemFormatter(), $href, $text, $depth, $subitems ); |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return callable |
||
57 | */ |
||
58 | public function getMenuItemFormatter() { |
||
59 | |||
60 | if ( $this->menuItemFormatter === null ) { |
||
61 | |||
62 | $this->setMenuItemFormatter( function ( $href, $text, $depth, $subitems ) { |
||
63 | $href = \Sanitizer::cleanUrl( $href ); |
||
64 | $text = htmlspecialchars( $text ); |
||
65 | $indent = str_repeat( "\t", 2 * $depth ); |
||
66 | |||
67 | if ( $subitems !== '' ) { |
||
68 | return "$indent<li>\n$indent\t<a href=\"$href\">$text</a>\n$subitems$indent</li>\n"; |
||
69 | } else { |
||
70 | return "$indent<li><a href=\"$href\">$text</a></li>\n"; |
||
71 | } |
||
72 | } ); |
||
73 | |||
74 | } |
||
75 | |||
76 | return $this->menuItemFormatter; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param callable $menuItemFormatter |
||
81 | */ |
||
82 | public function setMenuItemFormatter( $menuItemFormatter ) { |
||
85 | |||
86 | /** |
||
87 | * @param string $rawItemsHtml |
||
88 | * @param int $depth |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 1 | protected function getHtmlForMenuItemList( $rawItemsHtml, $depth ) { |
|
95 | |||
96 | /** |
||
97 | * @return callable |
||
98 | */ |
||
99 | public function getItemListFormatter() { |
||
110 | |||
111 | /** |
||
112 | * @param callable $itemListFormatter |
||
113 | */ |
||
114 | public function setItemListFormatter( $itemListFormatter ) { |
||
117 | |||
118 | } |
||
119 |