Skip to content

Commit

Permalink
Update query function
Browse files Browse the repository at this point in the history
  • Loading branch information
HillLiu committed May 24, 2016
1 parent f07eae9 commit 106defa
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
18 changes: 9 additions & 9 deletions algolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand All @@ -34,16 +34,16 @@ 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=[])
{
$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],[]),
Expand Down
30 changes: 25 additions & 5 deletions demo.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<?php
PMVC\Load::plug(['dotenv'=>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);


26 changes: 19 additions & 7 deletions src/BaseAlgolia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 106defa

Please sign in to comment.