Completed
Push — master ( b3219e...613cdf )
by Jean-Christophe
03:22
created

getElementByPropertyValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
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
 *
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->properties;
19
	}
20
21
	/**
22
	 * @param array $properties
23
	 * @return BaseHtml
24
	 */
25
	public function setProperties($properties) {
26
		$this->properties=$properties;
27
		return $this;
28
	}
29
30
	public function setProperty($name, $value) {
31
		$this->properties[$name]=$value;
32
		return $this;
33
	}
34
35
	public function getProperty($name) {
36
		if (array_key_exists($name, $this->properties))
37
			return $this->properties[$name];
38
	}
39
40
	public function addToProperty($name, $value, $separator=" ") {
41
		if (\is_array($value)) {
42
			foreach ( $value as $v ) {
43
				$this->addToProperty($name, $v, $separator);
44
			}
45
		} else if ($value !== "" && $this->propertyContains($name, $value) === false) {
46
			$v=@$this->properties[$name];
47
			if (isset($v) && $v !== "")
48
				$v=$v . $separator . $value;
49
				else
50
					$v=$value;
51
52
					return $this->setProperty($name, $v);
53
		}
54
		return $this;
55
	}
56
57
	public function addProperties($properties) {
58
		$this->properties=array_merge($this->properties, $properties);
59
		return $this;
60
	}
61
62
	protected function removePropertyValue($name, $value) {
63
		$this->properties[$name]=\str_replace($value, "", $this->properties[$name]);
64
		return $this;
65
	}
66
67
	protected function removePropertyValues($name, $values) {
68
		$this->removeOldValues($this->properties[$name], $values);
69
		return $this;
70
	}
71
72
	protected function addToPropertyUnique($name, $value, $typeCtrl) {
73
		if (@class_exists($typeCtrl, true))
74
			$typeCtrl=$typeCtrl::getConstants();
75
			if (\is_array($typeCtrl)) {
76
				$this->removeOldValues($this->properties[$name], $typeCtrl);
77
			}
78
			return $this->addToProperty($name, $value);
79
	}
80
81
	public function addToPropertyCtrl($name, $value, $typeCtrl) {
82
		return $this->addToPropertyUnique($name, $value, $typeCtrl);
83
	}
84
85
	public function addToPropertyCtrlCheck($name, $value, $typeCtrl) {
86
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
87
			return $this->addToProperty($name, $value);
88
		}
89
		return $this;
90
	}
91
92
	public function removeProperty($name) {
93
		if (\array_key_exists($name, $this->properties))
94
			unset($this->properties[$name]);
95
			return $this;
96
	}
97
98
	public function propertyContains($propertyName, $value) {
99
		$values=$this->getProperty($propertyName);
100
		if (isset($values)) {
101
			return JString::contains($values, $value);
102
		}
103
		return false;
104
	}
105
106
	protected function setPropertyCtrl($name, $value, $typeCtrl) {
107
		if ($this->ctrl($name, $value, $typeCtrl) === true)
108
			return $this->setProperty($name, $value);
109
			return $this;
110
	}
111
112
	protected function getElementByPropertyValue($propertyName,$value, $elements) {
113
		return $this->_getElementBy(function($element) use ($propertyName,$value){return $element->propertyContains($propertyName, $value) === true;}, $elements);
114
	}
115
}