Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

includes/Admin/View/Helper/Notification.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\Admin\View\Helper;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Html;
6
use Redaxscript\Language;
7
use Redaxscript\Module;
8
use function array_key_exists;
9
use function array_replace_recursive;
10
use function is_array;
11
12
/**
13
 * helper class to create the admin notification
14
 *
15
 * @since 4.0.0
16
 *
17
 * @package Redaxscript
18
 * @category View
19
 * @author Henry Ruhs
20
 */
21
22
class Notification implements Admin\View\ViewInterface
23
{
24
	/**
25
	 * instance of the language class
26
	 *
27
	 * @var Language
28
	 */
29
30
	protected $_language;
31
32
	/**
33
	 * array of the notification
34
	 *
35
	 * @var array
36
	 */
37
38
	protected $_notificationArray = [];
39
40
	/**
41
	 * options of the notification
42
	 *
43
	 * @var array
44
	 */
45
46
	protected $_optionArray =
47
	[
48
		'className' =>
49
		[
50
			'list' => 'rs-admin-list-notification',
51
			'item' => 'rs-admin-item-notification rs-admin-item-note',
52
			'title' => 'rs-admin-title-notification',
53
			'link' => 'rs-admin-link-notification',
54
			'text' => 'rs-admin-text-notification',
55
			'note' =>
56
			[
57
				'success' => 'rs-admin-is-success',
58
				'warning' => 'rs-admin-is-warning',
59
				'error' => 'rs-admin-is-error',
60
				'info' => 'rs-admin-is-info'
61
			]
62
		]
63
	];
64
65
	/**
66
	 * constructor of the class
67
	 *
68
	 * @since 4.0.0
69
	 *
70
	 * @param Language $language instance of the language class
71
	 */
72
73 1
	public function __construct(Language $language)
74
	{
75 1
		$this->_language = $language;
76 1
	}
77
78
	/**
79
	 * init the class
80
	 *
81
	 * @since 4.0.0
82
	 *
83
	 * @param array $optionArray options of the notification
84
	 *
85
	 * @return self
86 1
	 */
87
88 1
	public function init(array $optionArray = []) : self
89 1
	{
90 1
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
91
		$this->_notificationArray = Module\Hook::collect('adminNotification');
92
		return $this;
93
	}
94
95
	/**
96
	 * render the view
97
	 *
98
	 * @since 4.0.0
99
	 *
100 1
	 * @return string|null
101
	 */
102 1
103 1
	public function render() : ?string
104
	{
105
		$output = null;
106
		$outputItem = null;
107 1
108
		/* html element */
109 1
110 1
		$element = new Html\Element();
111
		$titleElement = $element
112 1
			->copy()
113
			->init('h3',
114
			[
115 1
				'class' => $this->_optionArray['className']['title']
116 1
			]);
117
		$listElement = $element
118 1
			->copy()
119
			->init('ul',
120
			[
121 1
				'class' => $this->_optionArray['className']['list']
122 1
			]);
123
		$itemElement = $element
124 1
			->copy()
125
			->init('li',
126
			[
127 1
				'class' => $this->_optionArray['className']['item']
128 1
			]);
129
		$linkElement = $element
130 1
			->copy()
131
			->init('a',
132
			[
133 1
				'class' => $this->_optionArray['className']['link']
134 1
			]);
135
		$textElement = $element
136 1
			->copy()
137
			->init('span',
138
			[
139
				'class' => $this->_optionArray['className']['text']
140
			]);
141 1
142
		/* process notification */
143 1
144
		foreach ($this->_notificationArray as $typeKey => $typeValue)
145 1
		{
146
			foreach ($typeValue as $notificationKey => $notificationValue)
147
			{
148 1
				foreach ($notificationValue as $value)
149 1
				{
150 1
					$outputItem .= $itemElement
151 1
						->copy()
152
						->addClass($this->_optionArray['className']['note'][$typeKey])
153 1
						->html(
154 1
							$titleElement->text($notificationKey)
155 1
						)
156 1
						->append(
157
							is_array($value) &&
158
							array_key_exists('text', $value) &&
159
							array_key_exists('attr', $value) ? $linkElement->attr($value['attr'])->text($value['text']) : $textElement->text($value)
160
						);
161 1
				}
162
			}
163 1
		}
164
		if ($outputItem)
0 ignored issues
show
Bug Best Practice introduced by
The expression $outputItem of type string|null 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...
165 1
		{
166
			$output .= $listElement->html($outputItem);
167
		}
168
		return $output;
169
	}
170
171
	/**
172
	 * get the meta array
173
	 *
174
	 * @since 4.0.0
175
	 *
176
	 * @return array
177
	 */
178
179
	public function getMetaArray() : array
180
	{
181
		$metaArray = [];
182
183
		/* process notification */
184
185
		foreach ($this->_notificationArray as $typeKey => $typeValue)
186
		{
187
			foreach ($typeValue as $notificationKey => $notificationValue)
188
			{
189
				$metaArray[$typeKey]++;
190
				$metaArray['total']++;
191
			}
192
		}
193
		return $metaArray;
194
	}
195
}
196