Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Controller/Search.php (1 issue)

Check for implicit conversion of array to boolean.

Best Practice Bug Minor

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)
0 ignored issues
show
Bug Best Practice introduced by
The expression $validateArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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)
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
		$searchFilter = new Filter\Search();
94 8
		$secondParameter = $searchFilter->sanitize($this->_registry->get('secondParameter'));
95 8
		$thirdParameter = $searchFilter->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 3
					$secondParameter
114
				],
115 3
				'search' => str_replace('-', ' ', $thirdParameter)
116
			];
117
		}
118 1
		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
		$searchValidator = new Validator\Search();
134 8
		$validateArray = [];
135
136
		/* validate query */
137
138 8
		if (!$searchValidator->validate($queryArray['search']))
139
		{
140 2
			$validateArray[] = $this->_language->get('input_incorrect');
141
		}
142 8
		return $validateArray;
143
	}
144
145
	/**
146
	 * search in tables
147
	 *
148
	 * @since 3.0.0
149
	 *
150
	 * @param array $searchArray array of the search
151
	 *
152
	 * @return array
153
	 */
154
155 6
	protected function _search(array $searchArray = []) : array
156
	{
157 6
		$searchModel = new Model\Search();
158 6
		$resultArray = [];
159
160
		/* process table */
161
162 6
		if (is_array($searchArray['table']))
163
		{
164 6
			foreach ($searchArray['table'] as $table)
165
			{
166 6
				$resultArray[$table] = $searchModel->getByTable($table, $searchArray['search'], $searchArray['language']);
167
			}
168
		}
169 6
		return $resultArray;
170
	}
171
172
	/**
173
	 * render the result
174
	 *
175
	 * @since 3.0.0
176
	 *
177
	 * @param array $resultArray array of the result
178
	 *
179
	 * @return string
180
	 */
181
182 6
	protected function _renderResult(array $resultArray = []) : string
183
	{
184 6
		$searchList = new View\ResultList($this->_registry, $this->_language);
185 6
		return $searchList->render($resultArray);
186
	}
187
188
	/**
189
	 * show the info
190
	 *
191
	 * @since 3.0.0
192
	 *
193
	 * @param array $infoArray array of the info
194
	 *
195
	 * @return string
196
	 */
197
198 4
	protected function _info(array $infoArray = []) : string
199
	{
200 4
		$messenger = $this->_messengerFactory();
201
		return $messenger
202 4
			->setUrl($this->_language->get('back'), $this->_registry->get('root'))
203 4
			->info($infoArray['message'], $this->_language->get('something_wrong'));
204
	}
205
206
}
207