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