Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
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
	 * array of the notification
26
	 *
27
	 * @var array
28
	 */
29
30
	protected array $_notificationArray = [];
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
31
32
	/**
33
	 * options of the notification
34
	 *
35
	 * @var array
36
	 */
37
38
	protected array $_optionArray =
39
	[
40
		'className' =>
41
		[
42
			'list' => 'rs-admin-list-notification',
43
			'item' => 'rs-admin-item-notification rs-admin-item-note',
44
			'title' => 'rs-admin-title-notification',
45
			'link' => 'rs-admin-link-notification',
46
			'text' => 'rs-admin-text-notification',
47
			'note' =>
48
			[
49
				'success' => 'rs-admin-is-success',
50
				'warning' => 'rs-admin-is-warning',
51
				'error' => 'rs-admin-is-error',
52
				'info' => 'rs-admin-is-info'
53
			]
54
		]
55
	];
56
57
	/**
58
	 * constructor of the class
59
	 *
60
	 * @since 4.0.0
61
	 *
62
	 * @param Language $_language instance of the language class
63
	 */
64
65
	public function __construct(protected Language $_language)
66
	{
67
	}
68
69
	/**
70
	 * init the class
71
	 *
72
	 * @since 4.0.0
73 1
	 *
74
	 * @param array $optionArray options of the notification
75 1
	 *
76 1
	 * @return self
77
	 */
78
79
	public function init(array $optionArray = []) : self
80
	{
81
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
82
		$this->_notificationArray = Module\Hook::collect('adminNotification');
83
		return $this;
84
	}
85
86
	/**
87
	 * get the notification array
88 1
	 *
89
	 * @since 4.0.0
90 1
	 *
91 1
	 * @return array
92 1
	 */
93
94
	public function getNotificationArray() : array
95
	{
96
		return $this->_notificationArray;
97
	}
98
99
	/**
100
	 * render the view
101
	 *
102
	 * @since 4.0.0
103
	 *
104
	 * @return string|null
105
	 */
106
107
	public function render() : ?string
108
	{
109
		$output = null;
110
		$outputItem = null;
111
112
		/* html element */
113
114
		$element = new Html\Element();
115
		$titleElement = $element
116 1
			->copy()
117
			->init('h3',
118 1
			[
119 1
				'class' => $this->_optionArray['className']['title']
120
			]);
121
		$listElement = $element
122
			->copy()
123 1
			->init('ul',
124
			[
125 1
				'class' => $this->_optionArray['className']['list']
126 1
			]);
127
		$itemElement = $element
128 1
			->copy()
129
			->init('li',
130
			[
131 1
				'class' => $this->_optionArray['className']['item']
132 1
			]);
133
		$linkElement = $element
134 1
			->copy()
135
			->init('a',
136
			[
137 1
				'class' => $this->_optionArray['className']['link']
138 1
			]);
139
		$textElement = $element
140 1
			->copy()
141
			->init('span',
142
			[
143 1
				'class' => $this->_optionArray['className']['text']
144 1
			]);
145
146 1
		/* process notification */
147
148
		foreach ($this->_notificationArray as $typeKey => $typeValue)
149 1
		{
150 1
			foreach ($typeValue as $notificationKey => $notificationValue)
151
			{
152 1
				foreach ($notificationValue as $value)
153
				{
154
					$outputItem .= $itemElement
155
						->copy()
156
						->addClass($this->_optionArray['className']['note'][$typeKey])
157 1
						->html(
158
							$titleElement->text($notificationKey)
159 1
						)
160
						->append(
161 1
							is_array($value) && array_key_exists('text', $value) && array_key_exists('attr', $value) ?
162
								$linkElement->attr($value['attr'])->text($value['text']) :
163
								$textElement->text($value)
164 1
						);
165 1
				}
166 1
			}
167 1
		}
168
		if ($outputItem)
169 1
		{
170 1
			$output .= $listElement->html($outputItem);
171 1
		}
172 1
		return $output;
173
	}
174
}
175