BaseHtmlPropertiesTrait::addToProperty()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 10
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 15
rs 8.4444
1
<?php
2
namespace Ajax\common\html\traits;
3
4
use Ajax\service\JString;
5
use Ajax\common\html\BaseHtml;
6
7
/**
8
 * @author jc
9
 * @property BaseWidget $_self
10
 */
11
trait BaseHtmlPropertiesTrait{
12
13
	protected $properties=array ();
14
	abstract protected function ctrl($name, $value, $typeCtrl);
15
	abstract protected function removeOldValues(&$oldValue, $allValues);
16
	abstract protected function _getElementBy($callback,$elements);
17
	public function getProperties() {
18
		return $this->_self->properties;
19
	}
20
21
	/**
22
	 * @param array $properties
23
	 * @return $this
24
	 */
25
	public function setProperties($properties) {
26
		$this->_self->properties=$properties;
27
		return $this;
28
	}
29
30
	public function setProperty($name, $value) {
31
		$this->_self->properties[$name]=$value;
32
		return $this;
33
	}
34
35
	public function getProperty($name) {
36
		if (array_key_exists($name, $this->_self->properties))
37
			return $this->_self->properties[$name];
38
	}
39
40
	/**
41
	 * @param string $name
42
	 * @param mixed $value
43
	 * @param string $separator
44
	 * @return $this
45
	 */
46
	public function addToProperty($name, $value, $separator=" ") {
47
		if (\is_array($value)) {
48
			foreach ( $value as $v ) {
49
				$this->_self->addToProperty($name, $v, $separator);
50
			}
51
		} else if ($value !== "" && $this->_self->propertyContains($name, $value) === false) {
52
			if(isset($this->_self->properties[$name])){
53
				$v=$this->_self->properties[$name];
54
				if (isset($v) && $v !== ""){
55
					$value=$v . $separator . $value;
56
				}
57
			}
58
			return $this->_self->setProperty($name, $value);
59
		}
60
		return $this;
61
	}
62
63
	public function addProperties($properties) {
64
		$this->_self->properties=array_merge($this->_self->properties, $properties);
65
		return $this;
66
	}
67
68
	public function removePropertyValue($name, $value) {
69
		$this->_self->properties[$name]=\str_replace($value, "", $this->_self->properties[$name]);
70
		return $this;
71
	}
72
73
	protected function removePropertyValues($name, $values) {
74
		$this->_self->removeOldValues($this->_self->properties[$name], $values);
75
		return $this;
76
	}
77
78
	protected function addToPropertyUnique($name, $value, $typeCtrl) {
79
		if (is_string($typeCtrl) && @class_exists($typeCtrl, true))
80
			$typeCtrl=$typeCtrl::getConstants();
81
			if (\is_array($typeCtrl)) {
82
				$this->_self->removeOldValues($this->_self->properties[$name], $typeCtrl);
83
			}
84
			return $this->_self->addToProperty($name, $value);
85
	}
86
87
	public function addToPropertyCtrl($name, $value, $typeCtrl) {
88
		return $this->_self->addToPropertyUnique($name, $value, $typeCtrl);
89
	}
90
91
	public function addToPropertyCtrlCheck($name, $value, $typeCtrl) {
92
		if ($this->_self->ctrl($name, $value, $typeCtrl) === true) {
93
			return $this->_self->addToProperty($name, $value);
94
		}
95
		return $this;
96
	}
97
98
	public function removeProperty($name) {
99
		if (\array_key_exists($name, $this->_self->properties))
100
			unset($this->_self->properties[$name]);
101
			return $this;
102
	}
103
	
104
	public function propertyContains($propertyName, $value) {
105
		$values=$this->_self->getProperty($propertyName);
106
		if (isset($values) && $value!=null) {
107
			return JString::contains($values, $value);
108
		}
109
		return false;
110
	}
111
112
	protected function setPropertyCtrl($name, $value, $typeCtrl) {
113
		if ($this->_self->ctrl($name, $value, $typeCtrl) === true)
114
			return $this->_self->setProperty($name, $value);
115
			return $this;
116
	}
117
118
	protected function getElementByPropertyValue($propertyName,$value, $elements) {
119
		return $this->_self->_getElementBy(function(BaseHtml $element) use ($propertyName,$value){return $element->propertyContains($propertyName, $value) === true;}, $elements);
120
	}
121
}
122