Implemented search processor.

This commit is contained in:
Alex Tselegidis 2016-07-09 22:43:31 +02:00
parent 4a8111566d
commit f2e1af7cb4

View file

@ -15,6 +15,31 @@ namespace EA\Engine\Api\V1\Processors;
class Search implements ProcessorsInterface {
public static function process(array &$response) {
if (!isset($_GET['q'])) {
return;
}
$searchedResponse = [];
$keyword = (string)$_GET['q'];
foreach ($response as $entry) {
if (self::_recursiveArraySearch($entry, $keyword) !== false) {
$searchedResponse[] = $entry;
}
}
$response = $searchedResponse;
}
protected static function _recursiveArraySearch(array $haystack, $needle) {
foreach ($haystack as $key => $value) {
$currentKey = $key;
if ($needle === $value || (is_array($value) && self::_recursiveArraySearch($value, $needle) !== false)) {
return $currentKey;
}
}
return false;
}
}