-
Notifications
You must be signed in to change notification settings - Fork 1
/
Menu.php
42 lines (39 loc) · 1.37 KB
/
Menu.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
<?php
namespace App\Helpers;
/**
* creates a menu from a object
* usage example:
* $data['menu'] = Menu::getList($pages, $id = 'pageID', $title = 'pageTitle', $slug = 'pageSlug', $parent = 'pageParent');
*/
class Menu
{
/**
* getList creates a menu from a object
* @param object $rows object of keys/values
* @param int $id the id
* @param string $title name of the item
* @param string $slug url friendly name
* @param int $parent id of parent element
* @param int $depth level of the loop
* @return string formatted menu
*/
public static function getList($rows, $id, $title, $slug, $parent, $depth = 0)
{
// Start a new list
$newList = null;
//loop through the rows
foreach ($rows as $key => $row) {
if ($row->$parent == $depth) {
// Add an UL tag when LI exists
$newList == null ? $newList = "<ul>\n" : $newList;
$newList .= "<li><a href='$row->$slug'>".$row->$title."</a>";
// Find childrens
$newList .= self::getList(array_slice($rows, $key), $id, $title, $slug, $parent, $row->$id);
$newList .= "</li>\n";
}
}
// Add an UL end tag
strlen($newList) > 0 ? $newList .= "</ul>\n" : $newList;
return $newList;
}
}