This repository has been archived by the owner on Feb 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
html_element.php
153 lines (127 loc) · 2.91 KB
/
html_element.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
<?php namespace Squi;
/**
* Base class to add some common functionality
* for classes that represent HTML Elements.
*
* Incomplete - has no concept of the tag it represents
*
* Mostly valuable for its handling of the class attribute.
* A gotcha: retrieving the class by any means except for the
* attributes() method will return it as an array of classes.
*
* This would be fantastic as a trait in PHP 5.4 (ex. Attributable)
*/
class HTML_Element {
public $attr = array(
'class' => array(),
);
protected $no_value_attr = array(
'selected',
'checked',
'required',
);
protected static $accessible_attr = array();
/**
* Get or set the value of a single attribute (jQuery style)
*/
public function attr($attr, $value = null)
{
if (is_array($attr))
{
return $this->attributes($attr);
}
elseif (is_null($value))
{
return isset($this->attr[$attr]) ? $this->attr[$attr] : null;
}
if ($attr == 'class')
{
return $this->add_class($value);
}
$this->attr[$attr] = $value;
return $this;
}
/**
* Get or set an array of attributes
* The returned attributes are in HTML string format
*/
public function attributes($attributes = null)
{
if (is_array($attributes))
{
foreach ($attributes as $attr => $value)
{
$this->attr($attr, $value);
}
return $this;
}
return \HTML::attributes($this->attributes_array());
}
/**
* Get the attributes as an array
*/
public function attributes_array()
{
$attr = $this->attr;
// Convert the classes to string
if (isset($attr['class']) && is_array($attr['class']))
{
$attr['class'] = implode(' ', $attr['class']);
}
if (empty($attr['class'])) unset($attr['class']);
return $attr;
}
/**
* Add a class or array of classes
*/
public function add_class($class)
{
// Accept either string or array format
is_array($class) || $class = explode(' ', $class);
// Make sure attribute exists
isset($this->attr['class']) || $this->attr['class'] = array();
// Add new class(es) to attributes
$this->attr['class'] = array_merge($this->attr['class'], $class);
return $this;
}
/**
* Remove a class or an array of classes
*/
public function remove_class($class = null)
{
// Remove an array of classes
if (is_array($class))
{
array_walk($class, array($this, 'remove_class'));
return $this;
}
$index = array_search($class, $this->attr['class']);
if ($index !== false)
{
unset($this->attr['class'][$index]);
}
if (is_null($class) || empty($this->attr['class']))
{
unset($this->attr['class']);
}
return $this;
}
/**
* Helpers to translate accessible attributes
* to their actual attribute values
*/
public function __get($prop)
{
if (in_array($prop, static::$accessible_attr))
{
return $this->attr($prop);
}
}
public function __set($prop, $value)
{
if (in_array($prop, static::$accessible_attr))
{
return $this->attr($prop, $value);
}
}
}