1
|
|
|
<?php |
2
|
|
|
namespace Ajax\common\html\traits; |
3
|
|
|
|
4
|
|
|
use Ajax\service\JString; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author jc |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
trait BaseHtmlPropertiesTrait{ |
11
|
|
|
|
12
|
|
|
protected $properties=array (); |
13
|
|
|
abstract protected function ctrl($name, $value, $typeCtrl); |
14
|
|
|
public function getProperties() { |
15
|
|
|
return $this->properties; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function setProperties($properties) { |
19
|
|
|
$this->properties=$properties; |
20
|
|
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setProperty($name, $value) { |
24
|
|
|
$this->properties[$name]=$value; |
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getProperty($name) { |
29
|
|
|
if (array_key_exists($name, $this->properties)) |
30
|
|
|
return $this->properties[$name]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function addToProperty($name, $value, $separator=" ") { |
34
|
|
|
if (\is_array($value)) { |
35
|
|
|
foreach ( $value as $v ) { |
36
|
|
|
$this->addToProperty($name, $v, $separator); |
37
|
|
|
} |
38
|
|
|
} else if ($value !== "" && $this->propertyContains($name, $value) === false) { |
39
|
|
|
$v=@$this->properties[$name]; |
40
|
|
|
if (isset($v) && $v !== "") |
41
|
|
|
$v=$v . $separator . $value; |
42
|
|
|
else |
43
|
|
|
$v=$value; |
44
|
|
|
|
45
|
|
|
return $this->setProperty($name, $v); |
46
|
|
|
} |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function addProperties($properties) { |
51
|
|
|
$this->properties=array_merge($this->properties, $properties); |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function removePropertyValue($name, $value) { |
56
|
|
|
$this->properties[$name]=\str_replace($value, "", $this->properties[$name]); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function removePropertyValues($name, $values) { |
61
|
|
|
$this->removeOldValues($this->properties[$name], $values); |
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function addToPropertyUnique($name, $value, $typeCtrl) { |
66
|
|
|
if (@class_exists($typeCtrl, true)) |
67
|
|
|
$typeCtrl=$typeCtrl::getConstants(); |
68
|
|
|
if (\is_array($typeCtrl)) { |
69
|
|
|
$this->removeOldValues($this->properties[$name], $typeCtrl); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
return $this->addToProperty($name, $value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function addToPropertyCtrl($name, $value, $typeCtrl) { |
75
|
|
|
return $this->addToPropertyUnique($name, $value, $typeCtrl); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function addToPropertyCtrlCheck($name, $value, $typeCtrl) { |
79
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) { |
80
|
|
|
return $this->addToProperty($name, $value); |
81
|
|
|
} |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function removeProperty($name) { |
86
|
|
|
if (\array_key_exists($name, $this->properties)) |
87
|
|
|
unset($this->properties[$name]); |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function propertyContains($propertyName, $value) { |
92
|
|
|
$values=$this->getProperty($propertyName); |
93
|
|
|
if (isset($values)) { |
94
|
|
|
return JString::contains($values, $value); |
95
|
|
|
} |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function setPropertyCtrl($name, $value, $typeCtrl) { |
100
|
|
|
if ($this->ctrl($name, $value, $typeCtrl) === true) |
101
|
|
|
return $this->setProperty($name, $value); |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function getElementByPropertyValue($propertyName,$value, $elements) { |
106
|
|
|
if (\is_array($elements)) { |
107
|
|
|
$flag=false; |
108
|
|
|
$index=0; |
109
|
|
|
while ( !$flag && $index < sizeof($elements) ) { |
110
|
|
|
if ($elements[$index] instanceof BaseHtml) |
|
|
|
|
111
|
|
|
$flag=($elements[$index]->propertyContains($propertyName, $value) === true); |
112
|
|
|
$index++; |
113
|
|
|
} |
114
|
|
|
if ($flag === true) |
115
|
|
|
return $elements[$index - 1]; |
116
|
|
|
} elseif ($elements instanceof BaseHtml) { |
|
|
|
|
117
|
|
|
if ($elements->propertyContains($propertyName, $value) === true) |
118
|
|
|
return $elements; |
119
|
|
|
} |
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.