From 106defa39af20a73b4b7cd1cb02aeb2200285d91 Mon Sep 17 00:00:00 2001 From: Hill Date: Mon, 23 May 2016 09:25:36 +0000 Subject: [PATCH] Update query function --- algolia.php | 18 +++++++++--------- demo.php | 30 +++++++++++++++++++++++++----- src/BaseAlgolia.php | 26 +++++++++++++++++++------- test.php | 2 +- 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/algolia.php b/algolia.php index c9fb4d1..676ff63 100755 --- a/algolia.php +++ b/algolia.php @@ -7,18 +7,18 @@ \PMVC\l(__DIR__.'/src/BaseAlgolia.php'); /** - * @parameters string ALGOLIA_APP - * @parameters string ALGOLIA_KEY + * @parameters string app + * @parameters string key */ class algolia extends \IdOfThings\GetDb { public function init() { - if (!isset($this['ALGOLIA_APP'])) { - $this['ALGOLIA_APP'] = \PMVC\getOption('ALGOLIA_APP'); + if (!isset($this['app'])) { + $this['app'] = \PMVC\getOption('ALGOLIA_APP'); } - if (!isset($this['ALGOLIA_KEY'])) { - $this['ALGOLIA_KEY'] = \PMVC\getOption('ALGOLIA_KEY'); + if (!isset($this['key'])) { + $this['key'] = \PMVC\getOption('ALGOLIA_KEY'); } } @@ -34,7 +34,7 @@ public function getNameSpace() public function getBaseUrl() { - return 'https://'.$this['ALGOLIA_APP'].'.algolia.net/1'; + return 'https://'.$this['app'].'.algolia.net/1'; } public function request($url, $params=[]) @@ -42,8 +42,8 @@ public function request($url, $params=[]) $respond = null; $url->set($this->getBaseUrl()); $header = [ - 'X-Algolia-Application-Id: '.$this['ALGOLIA_APP'], - 'X-Algolia-API-Key: '.$this['ALGOLIA_KEY'] + 'X-Algolia-Application-Id: '.$this['app'], + 'X-Algolia-API-Key: '.$this['key'] ]; $params[CURLOPT_HTTPHEADER] = array_merge( \PMVC\value($params,[CURLOPT_HTTPHEADER],[]), diff --git a/demo.php b/demo.php index 7e3afbd..210a949 100644 --- a/demo.php +++ b/demo.php @@ -1,6 +1,26 @@ null]); -PMVC\addPlugInFolders(['../']); -\PMVC\plug('dotenv',[ - \PMVC\PlugIn\dotenv\EnvFile=>'./.env' -])->init(); +include_once('./vendor/autoload.php'); + +\PMVC\Load::plug(['dotenv'=>['./.env']]); +\PMVC\addPlugInFolders(['../']); + +$algo = \PMVC\plug('algolia'); +$park = $algo->getdb('parking'); + +$result = $park->search('"'.\PMVC\getOption('QUERY').'"',[ + 'minProximity'=>1, + 'minWordSizefor1Typo'=>1, + 'advancedSyntax'=>true, + 'attributes'=>'doc.name,doc.info', + 'attributesToHighlight'=>'doc.name', + 'restrictSearchableAttributes'=>'doc.name', + 'disableTypoToleranceOnAttributes'=>'doc.name', + 'queryType'=>'prefixNone', + 'distinct'=>true, + 'typoTolerance'=>'false', + 'removeStopWords'=>false +]); + +var_dump($result); + + diff --git a/src/BaseAlgolia.php b/src/BaseAlgolia.php index 5b30e15..b1e4795 100644 --- a/src/BaseAlgolia.php +++ b/src/BaseAlgolia.php @@ -92,17 +92,28 @@ public function replace($k, $v) return $result; } + /** + * Update + */ + public function update($k, $v) + { + return $this->replace($k,$v); + } + /** * Search */ - public function search($query) + public function search($query, array $params=[]) { $url = \PMVC\plug('url')->getUrl(INDEX_PATH); $url->set($this->groupId); - $url->query = [ - 'query'=>$query, - 'advancedSyntax'=>true + $default = [ + 'analytics'=>false ]; + if (!empty($query)) { + $default['query'] = $query; + } + $url->query = array_replace($default, $params); $result = \PMVC\plug('algolia')->request( $url ); @@ -150,9 +161,10 @@ public function offsetGet($k=null) $result = \PMVC\plug('algolia')->request( $url ); - $result = $result->body->doc; - if (is_object($result)) { - $result = (array)$result; + if (!isset($result->body->doc)) { + return null; + } else { + $result = (array)$result->body->doc; } } return $result; diff --git a/test.php b/test.php index cc62b68..b0437bc 100644 --- a/test.php +++ b/test.php @@ -2,7 +2,7 @@ namespace PMVC\PlugIn\algolia; use PHPUnit_Framework_TestCase; -\PMVC\Load::plug(['dotenv'=>null]); +\PMVC\Load::plug(); \PMVC\addPlugInFolders(['../']); class AlgoliaTest extends PHPUnit_Framework_TestCase