HeadAbstract   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 0
loc 182
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 9 2
A init() 0 9 2
A append() 0 17 4
A prepend() 0 17 4
A remove() 0 16 4
A clear() 0 5 1
A _getCollectionArray() 0 4 2
A _setCollectionArray() 0 4 1
1
<?php
2
namespace Redaxscript\Head;
3
4
use Redaxscript\Singleton;
5
use function array_key_exists;
6
use function array_map;
7
use function array_splice;
8
use function array_unshift;
9
use function is_array;
10
use function strlen;
11
use function trim;
12
use function ucfirst;
13
14
/**
15
 * abstract class to create a head class
16
 *
17
 * @since 3.0.0
18
 *
19
 * @package Redaxscript
20
 * @category Head
21
 * @author Henry Ruhs
22
 */
23
24
abstract class HeadAbstract extends Singleton implements HeadInterface
25
{
26
	/**
27
	 * collection namespace
28
	 *
29
	 * @var string
30
	 */
31
32
	protected static $_namespace = 'Redaxscript\Head';
33
34
	/**
35
	 * collection of the head
36
	 *
37
	 * @var array
38
	 */
39
40
	protected static $_collectionArray = [];
41
42
	/**
43
	 * stringify the collection
44
	 *
45
	 * @since 3.0.0
46
	 *
47
	 * @return string
48
	 */
49 25
50
	public function __toString() : string
51 25
	{
52 25
		$render = $this->render();
53
		if ($render)
54 16
		{
55
			return $render;
56 9
		}
57
		return '<!-- ' . self::$_namespace . ' -->';
58
	}
59
60
	/**
61
	 * init the class
62
	 *
63
	 * @param string $namespace collection sub namespace
64
	 *
65
	 * @since 3.0.0
66
	 *
67
	 * @return self
68
	 */
69 26
70
	public function init(string $namespace = null) : self
71 26
	{
72 26
		self::$_namespace = static::class;
73
		if ($namespace)
0 ignored issues
show
Bug Best Practice introduced by
The expression $namespace of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
74 26
		{
75
			self::$_namespace .= '\\' . ucfirst($namespace);
76 26
		}
77
		return $this;
78
	}
79
80
	/**
81
	 * append to the collection
82
	 *
83
	 * @since 3.0.0
84
	 *
85
	 * @param string|array $attribute key or array of attributes
86
	 * @param string $value value of the attribute
87
	 *
88
	 * @return self
89
	 */
90 8
91
	public function append($attribute = null, string $value = null) : self
92 8
	{
93 8
		$collectionArray = $this->_getCollectionArray();
94
		if (is_array($attribute))
95 8
		{
96
			$collectionArray[] = array_map('trim', $attribute);
97 1
		}
98
		else if (strlen($attribute) && strlen($value))
99 1
		{
100
			$collectionArray[] =
101 1
			[
102
				trim($attribute) => trim($value)
103
			];
104 8
		}
105 8
		$this->_setCollectionArray($collectionArray);
106
		return $this;
107
	}
108
109
	/**
110
	 * prepend to the collection
111
	 *
112
	 * @since 3.0.0
113
	 *
114
	 * @param string|array $attribute key or array of attributes
115
	 * @param string $value value of the attribute
116
	 *
117
	 * @return self
118
	 */
119 4
120
	public function prepend($attribute = null, string $value = null) : self
121 4
	{
122 4
		$collectionArray = $this->_getCollectionArray();
123
		if (is_array($attribute))
124 4
		{
125
			array_unshift($collectionArray, array_map('trim', $attribute));
126 1
		}
127
		else if (strlen($attribute) && strlen($value))
128 1
		{
129
			array_unshift($collectionArray,
130 1
			[
131
				trim($attribute) => trim($value)
132
			]);
133 4
		}
134 4
		$this->_setCollectionArray($collectionArray);
135
		return $this;
136
	}
137
138
	/**
139
	 * remove from to the collection
140
	 *
141
	 * @since 3.0.0
142
	 *
143
	 * @param string $attribute name of attribute
144
	 * @param string $value value of the attribute
145
	 *
146
	 * @return self
147
	 */
148 2
149
	public function remove(string $attribute = null, string $value = null) : self
150 2
	{
151 2
		$collectionArray = $this->_getCollectionArray();
152
		if (is_array($collectionArray))
153 2
		{
154
			foreach ($collectionArray as $collectionKey => $collectionValue)
155 2
			{
156
				if ($collectionValue[$attribute] === $value)
157 2
				{
158
					array_splice($collectionArray, $collectionKey, 1);
159
				}
160 2
			}
161
			$this->_setCollectionArray($collectionArray);
162 2
		}
163
		return $this;
164
	}
165
166
	/**
167
	 * clear the collection
168
	 *
169
	 * @since 3.0.0
170
	 *
171
	 * @return self
172
	 */
173 25
174
	public function clear() : self
175 25
	{
176 25
		$this->_setCollectionArray();
177
		return $this;
178
	}
179
180
	/**
181
	 * get the collection array
182
	 *
183
	 * @since 3.0.0
184
	 *
185
	 * @return array
186
	 */
187 25
188
	protected function _getCollectionArray() : array
189 25
	{
190
		return array_key_exists(self::$_namespace, self::$_collectionArray) ? self::$_collectionArray[self::$_namespace] : [];
191
	}
192
193
	/**
194
	 * set the collection array
195
	 *
196
	 * @since 3.0.0
197
	 *
198
	 * @param array $collectionArray
199
	 */
200 25
201
	protected function _setCollectionArray(array $collectionArray = []) : void
202 25
	{
203 25
		self::$_collectionArray[self::$_namespace] = $collectionArray;
204
	}
205
}
206