Completed
Push — master ( e9cd30...c2303d )
by Henry
05:05
created

includes/Controller/Search.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\Controller;
3
4
use Redaxscript\Filter;
5
use Redaxscript\Model;
6
use Redaxscript\Validator;
7
use Redaxscript\View;
8
use function in_array;
9
use function is_array;
10
use function str_replace;
11
12
/**
13
 * children class to process the search request
14
 *
15
 * @since 3.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Controller
19
 * @author Henry Ruhs
20
 * @author Balázs Szilágyi
21
 */
22
23
class Search extends ControllerAbstract
24
{
25
	/**
26
	 * array of the tables
27
	 *
28
	 * @var array
29
	 */
30
31
	protected $tableArray =
32
	[
33
		'categories',
34
		'articles',
35
		'comments'
36
	];
37
38
	/**
39
	 * process the class
40
	 *
41
	 * @since 3.0.0
42
	 *
43
	 * @return string
44
	 */
45
46 8
	public function process() : string
47
	{
48 8
		$queryArray = $this->_sanitizeQuery();
49
50
		/* validate query */
51
52 8
		$validateArray = $this->_validateQuery($queryArray);
53 8
		if ($validateArray)
54
		{
55 2
			return $this->_info(
56
			[
57 2
				'message' => $validateArray
58
			]);
59
		}
60
61
		/* handle search */
62
63 6
		$resultArray = $this->_search(
64
		[
65 6
			'table' => $queryArray['table'],
66 6
			'search' => $queryArray['search'],
67 6
			'language' => $this->_registry->get('language')
68
		]);
69 6
		$output = $resultArray ? $this->_renderResult($resultArray) : null;
70 6
		if ($output)
0 ignored issues
show
Bug Best Practice introduced by
The expression $output 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...
71
		{
72 4
			return $output;
73
		}
74
75
		/* handle info */
76
77 2
		return $this->_info(
78
		[
79 2
			'message' => $this->_language->get('search_no')
80
		]);
81
	}
82
83
	/**
84
	 * sanitize the query
85
	 *
86
	 * @since 4.0.0
87
	 *
88
	 * @return array
89
	 */
90
91 8
	protected function _sanitizeQuery() : array
92
	{
93 8
		$aliasFilter = new Filter\Alias();
94 8
		$secondParameter = $aliasFilter->sanitize($this->_registry->get('secondParameter'));
95 8
		$thirdParameter = $aliasFilter->sanitize($this->_registry->get('thirdParameter'));
96
97
		/* process query */
98
99 8
		if (!$thirdParameter)
100
		{
101
			return
102
			[
103 4
				'table' => $this->tableArray,
104 4
				'search' => str_replace('-', ' ', $secondParameter)
105
			];
106
		}
107 4
		if (in_array($secondParameter, $this->tableArray))
108
		{
109
			return
110
			[
111
				'table' =>
112
				[
113 4
					$secondParameter
114
				],
115 4
				'search' => str_replace('-', ' ', $thirdParameter)
116
			];
117
		}
118
		return [];
119
	}
120
121
	/**
122
	 * validate the query
123
	 *
124
	 * @since 3.0.0
125
	 *
126
	 * @param array $queryArray array of the query
127
	 *
128
	 * @return array
129
	 */
130
131 8
	protected function _validateQuery(array $queryArray = []) : array
132
	{
133 8
		$aliasValidator = new Validator\Alias();
134 8
		$validateArray = [];
135
136
		/* validate query */
137
138 8
		if (!$queryArray['search'])
139
		{
140 1
			$validateArray[] = $this->_language->get('input_empty');
141
		}
142 7
		else if (!$aliasValidator->validate($queryArray['search']))
143
		{
144 1
			$validateArray[] = $this->_language->get('input_incorrect');
145
		}
146 8
		return $validateArray;
147
	}
148
149
	/**
150
	 * search in tables
151
	 *
152
	 * @since 3.0.0
153
	 *
154
	 * @param array $searchArray array of the search
155
	 *
156
	 * @return array
157
	 */
158
159 6
	protected function _search(array $searchArray = []) : array
160
	{
161 6
		$searchModel = new Model\Search();
162 6
		$resultArray = [];
163
164
		/* process table */
165
166 6
		if (is_array($searchArray['table']))
167
		{
168 6
			foreach ($searchArray['table'] as $table)
169
			{
170 6
				$resultArray[$table] = $searchModel->getByTable($table, $searchArray['search'], $searchArray['language']);
171
			}
172
		}
173 6
		return $resultArray;
174
	}
175
176
	/**
177
	 * render the result
178
	 *
179
	 * @since 3.0.0
180
	 *
181
	 * @param array $resultArray array of the result
182
	 *
183
	 * @return string
184
	 */
185
186 6
	protected function _renderResult(array $resultArray = []) : string
187
	{
188 6
		$searchList = new View\ResultList($this->_registry, $this->_language);
189 6
		return $searchList->render($resultArray);
190
	}
191
192
	/**
193
	 * show the info
194
	 *
195
	 * @since 3.0.0
196
	 *
197
	 * @param array $infoArray array of the info
198
	 *
199
	 * @return string
200
	 */
201
202 4
	protected function _info(array $infoArray = []) : string
203
	{
204 4
		$messenger = $this->_messengerFactory();
205
		return $messenger
206 4
			->setUrl($this->_language->get('back'), $this->_registry->get('root'))
207 4
			->info($infoArray['message'], $this->_language->get('something_wrong'));
208
	}
209
210
}
211