Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Head/HeadAbstract.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Head;
3
4
use Redaxscript\Singleton;
5
use function array_map;
6
use function array_unshift;
7
use function is_array;
8
use function strlen;
9
use function trim;
10
use function ucfirst;
11
12
/**
13
 * abstract class to create a head class
14
 *
15
 * @since 3.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Head
19
 * @author Henry Ruhs
20
 */
21
22
abstract class HeadAbstract extends Singleton implements HeadInterface
23
{
24
	/**
25
	 * collection namespace
26
	 *
27
	 * @var string
28
	 */
29
30
	protected static $_namespace = 'Redaxscript\Head';
31
32
	/**
33
	 * collection of the head
34
	 *
35
	 * @var array
36
	 */
37
38
	protected static $_collectionArray = [];
39
40
	/**
41
	 * stringify the collection
42
	 *
43
	 * @since 3.0.0
44
	 *
45
	 * @return string
46
	 */
47
48 25
	public function __toString() : string
49
	{
50 25
		$render = $this->render();
51 25
		if ($render)
52
		{
53 16
			return $render;
54
		}
55 9
		return '<!-- ' . self::$_namespace . ' -->';
56
	}
57
58
	/**
59
	 * init the class
60
	 *
61
	 * @param string $namespace collection sub namespace
62
	 *
63
	 * @since 3.0.0
64
	 *
65
	 * @return self
66
	 */
67
68 26
	public function init(string $namespace = null) : self
69
	{
70 26
		self::$_namespace = static::class;
71 26
		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...
72
		{
73 26
			self::$_namespace .= '\\' . ucfirst($namespace);
74
		}
75 26
		return $this;
76
	}
77
78
	/**
79
	 * append to the collection
80
	 *
81
	 * @since 3.0.0
82
	 *
83
	 * @param string|array $attribute key or array of attributes
84
	 * @param string $value value of the attribute
85
	 *
86
	 * @return self
87
	 */
88
89 8
	public function append($attribute = null, string $value = null) : self
90
	{
91 8
		$collectionArray = $this->_getCollectionArray();
92 8
		if (is_array($attribute))
93
		{
94 8
			$collectionArray[] = array_map('trim', $attribute);
95
		}
96 1
		else if (strlen($attribute) && strlen($value))
97
		{
98 1
			$collectionArray[] =
99
			[
100 1
				trim($attribute) => trim($value)
101
			];
102
		}
103 8
		$this->_setCollectionArray($collectionArray);
104 8
		return $this;
105
	}
106
107
	/**
108
	 * prepend to the collection
109
	 *
110
	 * @since 3.0.0
111
	 *
112
	 * @param string|array $attribute key or array of attributes
113
	 * @param string $value value of the attribute
114
	 *
115
	 * @return self
116
	 */
117
118 4
	public function prepend($attribute = null, string $value = null) : self
119
	{
120 4
		$collectionArray = $this->_getCollectionArray();
121 4
		if (is_array($attribute))
122
		{
123 4
			array_unshift($collectionArray, array_map('trim', $attribute));
124
		}
125 1
		else if (strlen($attribute) && strlen($value))
126
		{
127 1
			array_unshift($collectionArray,
128
			[
129 1
				trim($attribute) => trim($value)
130
			]);
131
		}
132 4
		$this->_setCollectionArray($collectionArray);
133 4
		return $this;
134
	}
135
136
	/**
137
	 * remove from to the collection
138
	 *
139
	 * @since 3.0.0
140
	 *
141
	 * @param string $attribute name of attribute
142
	 * @param string $value value of the attribute
143
	 *
144
	 * @return self
145
	 */
146
147 2
	public function remove(string $attribute = null, string $value = null) : self
148
	{
149 2
		$collectionArray = $this->_getCollectionArray();
150 2
		if (is_array($collectionArray))
151
		{
152 2
			foreach ($collectionArray as $collectionKey => $collectionValue)
153
			{
154 2
				if ($collectionValue[$attribute] === $value)
155
				{
156 2
					unset($collectionArray[$collectionKey]);
157
				}
158
			}
159 2
			$this->_setCollectionArray($collectionArray);
160
		}
161 2
		return $this;
162
	}
163
164
	/**
165
	 * clear the collection
166
	 *
167
	 * @since 3.0.0
168
	 *
169
	 * @return self
170
	 */
171
172 25
	public function clear() : self
173
	{
174 25
		$this->_setCollectionArray();
175 25
		return $this;
176
	}
177
178
	/**
179
	 * get the collection array
180
	 *
181
	 * @since 3.0.0
182
	 *
183
	 * @return array
184
	 */
185
186 25
	protected function _getCollectionArray() : array
187
	{
188 25
		$collectionArray = self::$_collectionArray[self::$_namespace];
189 25
		return is_array($collectionArray) ? $collectionArray : [];
190
	}
191
192
	/**
193
	 * set the collection array
194
	 *
195
	 * @since 3.0.0
196
	 *
197
	 * @param array $collectionArray
198
	 */
199
200 25
	protected function _setCollectionArray(array $collectionArray = []) : void
201
	{
202 25
		self::$_collectionArray[self::$_namespace] = $collectionArray;
203 25
	}
204
}
205