Skip to content

Commit b65ced6

Browse files
committed
BaseHtmlProperties trait
1 parent 6831dbd commit b65ced6

File tree

2 files changed

+124
-105
lines changed

2 files changed

+124
-105
lines changed

Ajax/common/html/BaseHtml.php

Lines changed: 2 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
use Ajax\common\components\SimpleExtComponent;
88
use Ajax\JsUtils;
99
use Ajax\common\html\traits\BaseHtmlEventsTrait;
10+
use Ajax\common\html\traits\BaseHtmlPropertiesTrait;
1011

1112
/**
1213
* BaseHtml for HTML components
1314
* @author jc
1415
* @version 1.2
1516
*/
1617
abstract class BaseHtml extends BaseWidget {
17-
use BaseHtmlEventsTrait;
18+
use BaseHtmlEventsTrait,BaseHtmlPropertiesTrait;
1819
protected $_template;
1920
protected $tagName;
20-
protected $properties=array ();
2121
protected $_wrapBefore=array ();
2222
protected $_wrapAfter=array ();
2323
protected $_bsComponent;
@@ -35,47 +35,6 @@ protected function getTemplate(JsUtils $js=NULL) {
3535
return PropertyWrapper::wrap($this->_wrapBefore, $js) . $this->_template . PropertyWrapper::wrap($this->_wrapAfter, $js);
3636
}
3737

38-
public function getProperties() {
39-
return $this->properties;
40-
}
41-
42-
public function setProperties($properties) {
43-
$this->properties=$properties;
44-
return $this;
45-
}
46-
47-
public function setProperty($name, $value) {
48-
$this->properties[$name]=$value;
49-
return $this;
50-
}
51-
52-
public function getProperty($name) {
53-
if (array_key_exists($name, $this->properties))
54-
return $this->properties[$name];
55-
}
56-
57-
public function addToProperty($name, $value, $separator=" ") {
58-
if (\is_array($value)) {
59-
foreach ( $value as $v ) {
60-
$this->addToProperty($name, $v, $separator);
61-
}
62-
} else if ($value !== "" && $this->propertyContains($name, $value) === false) {
63-
$v=@$this->properties[$name];
64-
if (isset($v) && $v !== "")
65-
$v=$v . $separator . $value;
66-
else
67-
$v=$value;
68-
69-
return $this->setProperty($name, $v);
70-
}
71-
return $this;
72-
}
73-
74-
public function addProperties($properties) {
75-
$this->properties=array_merge($this->properties, $properties);
76-
return $this;
77-
}
78-
7938
public function compile(JsUtils $js=NULL, &$view=NULL) {
8039
$result=$this->getTemplate($js);
8140
foreach ( $this as $key => $value ) {
@@ -110,19 +69,7 @@ protected function ctrl($name, $value, $typeCtrl) {
11069
return true;
11170
}
11271

113-
public function propertyContains($propertyName, $value) {
114-
$values=$this->getProperty($propertyName);
115-
if (isset($values)) {
116-
return JString::contains($values, $value);
117-
}
118-
return false;
119-
}
12072

121-
protected function setPropertyCtrl($name, $value, $typeCtrl) {
122-
if ($this->ctrl($name, $value, $typeCtrl) === true)
123-
return $this->setProperty($name, $value);
124-
return $this;
125-
}
12673

12774
protected function setMemberCtrl(&$name, $value, $typeCtrl) {
12875
if ($this->ctrl($name, $value, $typeCtrl) === true) {
@@ -139,21 +86,7 @@ protected function addToMemberUnique(&$name, $value, $typeCtrl, $separator=" ")
13986
return $this;
14087
}
14188

142-
protected function removePropertyValue($name, $value) {
143-
$this->properties[$name]=\str_replace($value, "", $this->properties[$name]);
144-
return $this;
145-
}
146-
147-
protected function removePropertyValues($name, $values) {
148-
$this->removeOldValues($this->properties[$name], $values);
149-
return $this;
150-
}
15189

152-
public function removeProperty($name) {
153-
if (\array_key_exists($name, $this->properties))
154-
unset($this->properties[$name]);
155-
return $this;
156-
}
15790

15891
protected function addToMemberCtrl(&$name, $value, $typeCtrl, $separator=" ") {
15992
if ($this->ctrl($name, $value, $typeCtrl) === true) {
@@ -170,25 +103,7 @@ protected function addToMember(&$name, $value, $separator=" ") {
170103
return $this;
171104
}
172105

173-
protected function addToPropertyUnique($name, $value, $typeCtrl) {
174-
if (@class_exists($typeCtrl, true))
175-
$typeCtrl=$typeCtrl::getConstants();
176-
if (\is_array($typeCtrl)) {
177-
$this->removeOldValues($this->properties[$name], $typeCtrl);
178-
}
179-
return $this->addToProperty($name, $value);
180-
}
181106

182-
public function addToPropertyCtrl($name, $value, $typeCtrl) {
183-
return $this->addToPropertyUnique($name, $value, $typeCtrl);
184-
}
185-
186-
public function addToPropertyCtrlCheck($name, $value, $typeCtrl) {
187-
if ($this->ctrl($name, $value, $typeCtrl) === true) {
188-
return $this->addToProperty($name, $value);
189-
}
190-
return $this;
191-
}
192107

193108
protected function removeOldValues(&$oldValue, $allValues) {
194109
$oldValue=str_ireplace($allValues, "", $oldValue);
@@ -278,24 +193,6 @@ public function getElementById($identifier, $elements) {
278193
return null;
279194
}
280195

281-
protected function getElementByPropertyValue($propertyName,$value, $elements) {
282-
if (\is_array($elements)) {
283-
$flag=false;
284-
$index=0;
285-
while ( !$flag && $index < sizeof($elements) ) {
286-
if ($elements[$index] instanceof BaseHtml)
287-
$flag=($elements[$index]->propertyContains($propertyName, $value) === true);
288-
$index++;
289-
}
290-
if ($flag === true)
291-
return $elements[$index - 1];
292-
} elseif ($elements instanceof BaseHtml) {
293-
if ($elements->propertyContains($propertyName, $value) === true)
294-
return $elements;
295-
}
296-
return null;
297-
}
298-
299196
public function __toString() {
300197
return $this->compile();
301198
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

0 commit comments

Comments
 (0)