Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

includes/Admin/View/Helper/Option.php (4 issues)

Check for loose comparison of integers.

Best Practice Bug Major

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 DateTimeZone;
5
use Redaxscript\Admin;
6
use Redaxscript\Filesystem;
7
use Redaxscript\Language;
8
use function function_exists;
9
use function mb_list_encodings;
10
use function resourcebundle_locales;
11
use function substr;
12
13
/**
14
 * helper class to create various options
15
 *
16
 * @since 3.0.0
17
 *
18
 * @package Redaxscript
19
 * @category View
20
 * @author Henry Ruhs
21
 */
22
23
class Option
24
{
25
	/**
26
	 * instance of the language class
27
	 *
28
	 * @var Language
29
	 */
30
31
	protected $_language;
32
33
	/**
34
	 * constructor of the class
35
	 *
36
	 * @since 3.2.0
37
	 *
38
	 * @param Language $language instance of the language class
39
	 */
40
41 12
	public function __construct(Language $language)
42
	{
43 12
		$this->_language = $language;
44 12
	}
45
46
	/**
47
	 * get the robot array
48
	 *
49
	 * @since 3.0.0
50
	 *
51
	 * @return array
52
	 */
53
54 1
	public function getRobotArray() : array
55
	{
56
		return
57
		[
58 1
			$this->_language->get('select') => 'null',
59 1
			$this->_language->get('all') => 1,
60 1
			$this->_language->get('index') => 2,
61 1
			$this->_language->get('follow') => 3,
62 1
			$this->_language->get('index_no') => 4,
63 1
			$this->_language->get('follow_no') => 5,
64 1
			$this->_language->get('none') => 6
65
		];
66
	}
67
68
	/**
69
	 * get the charset array
70
	 *
71
	 * @since 4.4.0
72
	 *
73
	 * @return array
74
	 */
75
76 1
	public function getCharsetArray() : array
77
	{
78 1
		return mb_list_encodings();
79
	}
80
81
	/**
82
	 * get the locale array
83
	 *
84
	 * @since 4.4.0
85
	 *
86
	 * @return array
87
	 */
88
89 1
	public function getLocaleArray() : array
90
	{
91 1
		return function_exists('resourcebundle_locales') ? resourcebundle_locales(null) : [];
92
	}
93
94
	/**
95
	 * get the zone array
96
	 *
97
	 * @since 4.0.0
98
	 *
99
	 * @return array
100
	 */
101
102 1
	public function getZoneArray() : array
103
	{
104 1
		return DateTimeZone::listIdentifiers();
105
	}
106
107
	/**
108
	 * get the time array
109
	 *
110
	 * @since 3.0.0
111
	 *
112
	 * @return array
113
	 */
114
115 1
	public function getTimeArray() : array
116
	{
117
		return
118
		[
119 1
			'24h' => 'H:i',
120
			'12h' => 'h:i a'
121
		];
122
	}
123
124
	/**
125
	 * get the date array
126
	 *
127
	 * @since 3.0.0
128
	 *
129
	 * @return array
130
	 */
131
132 1
	public function getDateArray() : array
133
	{
134
		return
135
		[
136 1
			'DD.MM.YYYY' => 'd.m.Y',
137
			'MM.DD.YYYY' => 'm.d.Y',
138
			'YYYY.MM.DD' => 'Y.m.d'
139
		];
140
	}
141
142
	/**
143
	 * get the order array
144
	 *
145
	 * @since 3.0.0
146
	 *
147
	 * @return array
148
	 */
149
150 1
	public function getOrderArray() : array
151
	{
152
		return
153
		[
154 1
			$this->_language->get('ascending') => 'asc',
155 1
			$this->_language->get('descending') => 'desc'
156
		];
157
	}
158
159
	/**
160
	 * get the captcha array
161
	 *
162
	 * @since 3.0.0
163
	 *
164
	 * @return array
165
	 */
166
167 1
	public function getCaptchaArray() : array
168
	{
169
		return
170
		[
171 1
			$this->_language->get('random') => 1,
172 1
			$this->_language->get('addition') => 2,
173 1
			$this->_language->get('subtraction') => 3,
174 1
			$this->_language->get('disable') => 0
175
		];
176
	}
177
178
	/**
179
	 * get the permission array
180
	 *
181
	 * @since 3.0.0
182
	 *
183
	 * @param string|null $table name of the table
184
	 *
185
	 * @return array
186
	 */
187
188 1
	public function getPermissionArray(?string $table) : array
189
	{
190 1
		if ($table === 'modules')
191
		{
192
			return
193
			[
194 1
				$this->_language->get('install') => 1,
195 1
				$this->_language->get('edit') => 2,
196 1
				$this->_language->get('uninstall') => 3
197
			];
198
		}
199 1
		if ($table === 'settings')
200
		{
201
			return
202
			[
203 1
				$this->_language->get('none') => 1,
204 1
				$this->_language->get('edit') => 2,
205
			];
206
		}
207
		return
208
		[
209 1
			$this->_language->get('create') => 1,
210 1
			$this->_language->get('edit') => 2,
211 1
			$this->_language->get('delete') => 3
212
		];
213
	}
214
215
	/**
216
	 * get the language array
217
	 *
218
	 * @since 3.0.0
219
	 *
220
	 * @return array
221
	 */
222
223 1
	public function getLanguageArray() : array
224
	{
225 1
		$languageFilesystem = new Filesystem\Filesystem();
226 1
		$languageFilesystem->init('languages');
227 1
		$languageFilesystemArray = $languageFilesystem->getSortArray();
228
		$languageArray =
229
		[
230 1
			$this->_language->get('select') => 'null'
231
		];
232
233
		/* process filesystem */
234
235 1
		foreach ($languageFilesystemArray as $value)
236
		{
237 1
			$value = substr($value, 0, 2);
238 1
			$languageArray[$this->_language->get('_language')[$value]] = $value;
239
		}
240 1
		return $languageArray;
241
	}
242
243
	/**
244
	 * get the template array
245
	 *
246
	 * @since 3.0.0
247
	 *
248
	 * @return array
249
	 */
250
251 1
	public function getTemplateArray() : array
252
	{
253 1
		$templateFilesystem = new Filesystem\Filesystem();
254 1
		$templateFilesystem->init('templates', false,
255
		[
256 1
			'admin',
257
			'console',
258
			'install'
259
		]);
260 1
		$templateFilesystemArray = $templateFilesystem->getSortArray();
261
		$templateArray =
262
		[
263 1
			$this->_language->get('select') => 'null'
264
		];
265
266
		/* process filesystem */
267
268 1
		foreach ($templateFilesystemArray as $value)
269
		{
270 1
			$templateArray[$value] = $value;
271
		}
272 1
		return $templateArray;
273
	}
274
275
	/**
276
	 * get the category array
277
	 *
278
	 * @since 4.5.0
279
	 *
280
	 * @return array
281
	 */
282
283
	public function getCategoryArray() : array
284
	{
285
		$categoryModel = new Admin\Model\Category();
286
		$contents = $categoryModel->query()->orderByAsc('title')->findMany();
287
		return $this->_getContentArray($contents);
288
	}
289
290
	/**
291
	 * get the sibling for category array
292
	 *
293
	 * @since 4.5.0
294
	 *
295
	 * @param int|null $categoryId identifier of the category
296
	 *
297
	 * @return array
298
	 */
299
300
	public function getSiblingForCategoryArray(?int $categoryId) : array
301
	{
302
		$categoryModel = new Admin\Model\Category();
303
		$query = $categoryModel->query();
304
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
305
		{
306
			$query->whereNotEqual('id', $categoryId);
307
		}
308
		$contents = $query->orderByAsc('title')->findMany();
309
		return $this->_getContentArray($contents);
310
	}
311
312
	/**
313
	 * get the parent for category array
314
	 *
315
	 * @since 4.5.0
316
	 *
317
	 * @param int|null $categoryId identifier of the category
318
	 *
319
	 * @return array
320
	 */
321
322
	public function getParentForCategoryArray(?int $categoryId) : array
323
	{
324
		$categoryModel = new Admin\Model\Category();
325
		$query = $categoryModel->query()->whereNull('parent');
326
		if ($categoryId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $categoryId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
327
		{
328
			$query->whereNotEqual('id', $categoryId);
329
		}
330
		$contents = $query->orderByAsc('title')->findMany();
331
		return $this->_getContentArray($contents);
332
	}
333
334
	/**
335
	 * get the article array
336
	 *
337
	 * @since 4.5.0
338
	 *
339
	 * @return array
340
	 */
341
342
	public function getArticleArray() : array
343
	{
344
		$articleModel = new Admin\Model\Article();
345
		$contents = $articleModel->query()->orderByAsc('title')->findMany();
346
		return $this->_getContentArray($contents);
347
	}
348
349
	/**
350
	 * get the sibling for article array
351
	 *
352
	 * @since 4.5.0
353
	 *
354
	 * @param int|null $articleId identifier of the article
355
	 *
356
	 * @return array
357
	 */
358
359
	public function getSiblingForArticleArray(?int $articleId) : array
360
	{
361
		$articleModel = new Admin\Model\Article();
362
		$query = $articleModel->query();
363
		if ($articleId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $articleId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
364
		{
365
			$query->whereNotEqual('id', $articleId);
366
		}
367
		$contents = $query->orderByAsc('title')->findMany();
368
		return $this->_getContentArray($contents);
369
	}
370
371
	/**
372
	 * get the sibling for extra array
373
	 *
374
	 * @since 4.5.0
375
	 *
376
	 * @param int|null $extraId identifier of the extra
377
	 *
378
	 * @return array
379
	 */
380
381
	public function getSiblingForExtraArray(?int $extraId) : array
382
	{
383
		$extraModel = new Admin\Model\Extra();
384
		$query = $extraModel->query();
385
		if ($extraId)
0 ignored issues
show
Bug Best Practice introduced by
The expression $extraId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
386
		{
387
			$query->whereNotEqual('id', $extraId);
388
		}
389
		$contents = $query->orderByAsc('title')->findMany();
390
		return $this->_getContentArray($contents);
391
	}
392
393
	/**
394
	 * get the article for comment array
395
	 *
396
	 * @since 4.5.0
397
	 *
398
	 * @return array
399
	 */
400
401
	public function getArticleForCommentArray() : array
402
	{
403
		$articleModel = new Admin\Model\Article();
404
		$contents = $articleModel->query()->where('comments', 1)->orderByAsc('title')->findMany();
405
		return $this->_getContentArray($contents);
406
	}
407
408
	/**
409
	 * get the content array
410
	 *
411
	 * @since 4.5.0
412
	 *
413
	 * @param object $contents
414
	 *
415
	 * @return array
416
	 */
417
418
	protected function _getContentArray(object $contents) : array
419
	{
420
		$contentArray =
421
		[
422
			$this->_language->get('select') => 'null'
423
		];
424
425
		/* process contents */
426
427
		foreach ($contents as $value)
428
		{
429
			$contentKey = $value->title . ' (' . $value->id . ')';
430
			$contentArray[$contentKey] = $value->id;
431
		}
432
		return $contentArray;
433
	}
434
435
	/**
436
	 * get the group array
437
	 *
438
	 * @since 4.0.0
439
	 *
440
	 * @return array
441
	 */
442
443 1
	public function getGroupArray() : array
444
	{
445 1
		$groupModel = new Admin\Model\Group();
446 1
		$groups = $groupModel->query()->orderByAsc('name')->findMany();
447 1
		$accessArray = [];
448
449
		/* process groups */
450
451 1
		foreach ($groups as $value)
452
		{
453 1
			$accessArray[$value->name] = $value->id;
454
		}
455 1
		return $accessArray;
456
	}
457
}
458