2016-07-09 10:02:25 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Easy!Appointments - Open Source Web Scheduler
|
|
|
|
*
|
|
|
|
* @package EasyAppointments
|
|
|
|
* @author A.Tselegidis <alextselegidis@gmail.com>
|
2017-01-31 09:35:34 +03:00
|
|
|
* @copyright Copyright (c) 2013 - 2017, Alex Tselegidis
|
2016-07-09 10:02:25 +03:00
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 - GPLv3
|
|
|
|
* @link http://easyappointments.org
|
|
|
|
* @since v1.2.0
|
|
|
|
* ---------------------------------------------------------------------------- */
|
|
|
|
|
2016-07-09 14:14:08 +03:00
|
|
|
namespace EA\Engine\Api\V1\Processors;
|
2016-07-09 10:02:25 +03:00
|
|
|
|
2016-07-10 11:59:39 +03:00
|
|
|
/**
|
|
|
|
* Search Processor
|
|
|
|
*
|
|
|
|
* This class will search the response with the "q" GET parameter and only provide the entries
|
|
|
|
* that match the keyword. Make sure that the response parameter is a sequential array and not
|
|
|
|
* a single entry by the time this processor is executed.
|
|
|
|
*/
|
2016-07-09 14:14:08 +03:00
|
|
|
class Search implements ProcessorsInterface {
|
2016-07-10 11:59:39 +03:00
|
|
|
/**
|
|
|
|
* Process Response Array
|
|
|
|
*
|
|
|
|
* @param array &$response The response array to be processed.
|
|
|
|
*/
|
2016-07-09 23:27:24 +03:00
|
|
|
public static function process(array &$response) {
|
2016-07-10 14:15:14 +03:00
|
|
|
if (!isset($_GET['q']) || empty($response)) {
|
2016-07-09 23:43:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$searchedResponse = [];
|
|
|
|
$keyword = (string)$_GET['q'];
|
|
|
|
|
|
|
|
foreach ($response as $entry) {
|
|
|
|
if (self::_recursiveArraySearch($entry, $keyword) !== false) {
|
|
|
|
$searchedResponse[] = $entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $searchedResponse;
|
|
|
|
}
|
|
|
|
|
2016-07-10 11:59:39 +03:00
|
|
|
/**
|
|
|
|
* Recursive Array Search
|
|
|
|
*
|
|
|
|
* @param array $haystack Array to search in.
|
|
|
|
* @param string $needle Keyword to be searched.
|
|
|
|
*
|
2016-11-06 20:30:03 +03:00
|
|
|
* @return int|bool Returns the index of the search occurrence or false it nothing was found.
|
2016-07-10 11:59:39 +03:00
|
|
|
*/
|
2016-07-09 23:43:31 +03:00
|
|
|
protected static function _recursiveArraySearch(array $haystack, $needle) {
|
|
|
|
foreach ($haystack as $key => $value) {
|
|
|
|
$currentKey = $key;
|
|
|
|
|
2016-07-10 14:16:52 +03:00
|
|
|
if (strpos($value, $needle) !== false || (is_array($value) && self::_recursiveArraySearch($value, $needle) !== false)) {
|
2016-07-09 23:43:31 +03:00
|
|
|
return $currentKey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-07-09 14:14:08 +03:00
|
|
|
}
|
2016-07-09 10:02:25 +03:00
|
|
|
}
|