|
1
|
|
|
<?php namespace Arcanedev\LaravelHtml\Helpers; |
|
2
|
|
|
|
|
3
|
|
|
class Lister |
|
4
|
|
|
{ |
|
5
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
6
|
|
|
| Main Functions |
|
7
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
8
|
|
|
*/ |
|
9
|
|
|
/** |
|
10
|
|
|
* Generate an ordered list of items. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $list |
|
13
|
|
|
* @param array $attributes |
|
14
|
|
|
* |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
16 |
|
public static function ol(array $list, array $attributes = []) |
|
18
|
|
|
{ |
|
19
|
16 |
|
return static::make('ol', $list, $attributes); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Generate an un-ordered list of items. |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $list |
|
26
|
|
|
* @param array $attributes |
|
27
|
|
|
* |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
16 |
|
public static function ul(array $list, array $attributes = []) |
|
31
|
|
|
{ |
|
32
|
16 |
|
return static::make('ul', $list, $attributes); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Generate a description list of items. |
|
37
|
|
|
* |
|
38
|
|
|
* @param array $list |
|
39
|
|
|
* @param array $attributes |
|
40
|
|
|
* |
|
41
|
|
|
* @return string |
|
42
|
|
|
*/ |
|
43
|
24 |
|
public static function dl(array $list, array $attributes = []) |
|
44
|
|
|
{ |
|
45
|
24 |
|
$html = ''; |
|
46
|
24 |
|
$attributes = Attributes::make($attributes); |
|
47
|
|
|
|
|
48
|
24 |
|
foreach ($list as $key => $value) { |
|
49
|
24 |
|
$html .= "<dt>$key</dt>"; |
|
50
|
24 |
|
$value = (array) $value; |
|
51
|
|
|
|
|
52
|
24 |
|
foreach ($value as $vKey => $vValue) { |
|
53
|
24 |
|
$html .= "<dd>$vValue</dd>"; |
|
54
|
18 |
|
} |
|
55
|
18 |
|
} |
|
56
|
|
|
|
|
57
|
16 |
|
return "<dl{$attributes}>{$html}</dl>"; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Create a listing HTML element. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $type |
|
64
|
|
|
* @param array $list |
|
65
|
|
|
* @param array $attributes |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
24 |
|
public static function make($type, array $list, array $attributes = []) |
|
70
|
|
|
{ |
|
71
|
24 |
|
if (count($list) == 0) return ''; |
|
72
|
|
|
|
|
73
|
24 |
|
$html = static::makeElements($type, $list); |
|
|
|
|
|
|
74
|
24 |
|
$attributes = Attributes::make($attributes); |
|
75
|
|
|
|
|
76
|
24 |
|
return "<{$type}{$attributes}>{$html}</{$type}>"; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
80
|
|
|
| Other Functions |
|
81
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
82
|
|
|
*/ |
|
83
|
|
|
/** |
|
84
|
|
|
* Make listing elements. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $type |
|
87
|
|
|
* @param array $list |
|
88
|
|
|
* |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
24 |
|
private static function makeElements($type, array $list) |
|
92
|
|
|
{ |
|
93
|
24 |
|
$html = ''; |
|
94
|
|
|
|
|
95
|
|
|
// Essentially we will just spin through the list and build the list of the HTML |
|
96
|
|
|
// elements from the array. We will also handled nested lists in case that is |
|
97
|
|
|
// present in the array. Then we will build out the final listing elements. |
|
98
|
24 |
|
foreach ($list as $key => $value) { |
|
99
|
24 |
|
$html .= static::makeElement($key, $type, $value); |
|
|
|
|
|
|
100
|
18 |
|
} |
|
101
|
|
|
|
|
102
|
24 |
|
return $html; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Create list element. |
|
107
|
|
|
* |
|
108
|
|
|
* @param mixed $key |
|
109
|
|
|
* @param string $type |
|
110
|
|
|
* @param mixed $value |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
24 |
|
private static function makeElement($key, $type, $value) |
|
115
|
|
|
{ |
|
116
|
24 |
|
return is_array($value) |
|
117
|
20 |
|
? static::makeNestedElements($key, $type, $value) |
|
|
|
|
|
|
118
|
24 |
|
: '<li>' . e($value) . '</li>'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Create a nested list attribute. |
|
123
|
|
|
* |
|
124
|
|
|
* @param mixed $key |
|
125
|
|
|
* @param string $type |
|
126
|
|
|
* @param mixed $value |
|
127
|
|
|
* |
|
128
|
|
|
* @return string |
|
129
|
|
|
*/ |
|
130
|
8 |
|
private static function makeNestedElements($key, $type, $value) |
|
131
|
|
|
{ |
|
132
|
8 |
|
return is_int($key) |
|
133
|
6 |
|
? static::make($type, $value) |
|
134
|
8 |
|
: '<li>' . $key . static::make($type, $value) . '</li>'; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: