-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from phiHero/master
retrieve preset by name, implement array access for presets, analyticsRules
- Loading branch information
Showing
5 changed files
with
182 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Typesense; | ||
|
||
use Http\Client\Exception as HttpClientException; | ||
use Typesense\Exceptions\TypesenseClientError; | ||
|
||
/** | ||
* Class Preset | ||
* | ||
* @package \Typesense | ||
*/ | ||
class Preset | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private string $presetName; | ||
|
||
/** | ||
* @var ApiCall | ||
*/ | ||
private ApiCall $apiCall; | ||
|
||
/** | ||
* Preset constructor. | ||
* | ||
* @param string $presetName | ||
* @param ApiCall $apiCall | ||
*/ | ||
public function __construct(string $presetName, ApiCall $apiCall) | ||
{ | ||
$this->presetName = $presetName; | ||
$this->apiCall = $apiCall; | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws TypesenseClientError|HttpClientException | ||
*/ | ||
public function retrieve(): array | ||
{ | ||
return $this->apiCall->get($this->endPointPath(), []); | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws TypesenseClientError|HttpClientException | ||
*/ | ||
public function delete(): array | ||
{ | ||
return $this->apiCall->delete($this->endPointPath()); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function endPointPath(): string | ||
{ | ||
return sprintf('%s/%s', Presets::PRESETS_PATH, $this->presetName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,13 @@ | |
use Typesense\Exceptions\TypesenseClientError; | ||
|
||
/** | ||
* Class Document | ||
* Class Presets | ||
* | ||
* @package \Typesense | ||
* @date 4/5/20 | ||
* @author Abdullah Al-Faqeir <[email protected]> | ||
*/ | ||
class Presets | ||
class Presets implements \ArrayAccess | ||
{ | ||
/** | ||
* @var ApiCall | ||
|
@@ -24,7 +24,12 @@ class Presets | |
public const MULTI_SEARCH_PATH = '/multi_search'; | ||
|
||
/** | ||
* Document constructor. | ||
* @var array | ||
*/ | ||
private array $presets = []; | ||
|
||
/** | ||
* Presets constructor. | ||
* | ||
* @param ApiCall $apiCall | ||
*/ | ||
|
@@ -49,37 +54,24 @@ public function searchWithPreset($presetName) | |
* @throws HttpClientException | ||
* @throws TypesenseClientError | ||
*/ | ||
public function get() | ||
public function retrieve() | ||
{ | ||
return $this->apiCall->get(static::PRESETS_PATH, []); | ||
} | ||
|
||
/** | ||
* @param string $presetName | ||
* @param array $options | ||
* | ||
* @return array | ||
* @throws HttpClientException | ||
* @throws TypesenseClientError | ||
*/ | ||
public function put(array $options = []) | ||
public function upsert(string $presetName, array $presetsData) | ||
{ | ||
$presetName = $options['preset_name']; | ||
$presetsData = $options['preset_data']; | ||
|
||
return $this->apiCall->put($this->endpointPath($presetName), $presetsData); | ||
} | ||
|
||
/** | ||
* @param $presetName | ||
* @return array | ||
* @throws HttpClientException | ||
* @throws TypesenseClientError | ||
*/ | ||
public function delete($presetName) | ||
{ | ||
return $this->apiCall->delete($this->endpointPath($presetName)); | ||
} | ||
|
||
/** | ||
* @param $presetsName | ||
* @return string | ||
|
@@ -104,4 +96,57 @@ private function multiSearchEndpointPath() | |
static::MULTI_SEARCH_PATH | ||
); | ||
} | ||
|
||
/** | ||
* @param $presetName | ||
* | ||
* @return mixed | ||
*/ | ||
public function __get($presetName) | ||
{ | ||
if (isset($this->{$presetName})) { | ||
return $this->{$presetName}; | ||
} | ||
if (!isset($this->presets[$presetName])) { | ||
$this->presets[$presetName] = new Preset($presetName, $this->apiCall); | ||
} | ||
|
||
return $this->presets[$presetName]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function offsetExists($offset): bool | ||
{ | ||
return isset($this->presets[$offset]); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function offsetGet($offset): Preset | ||
{ | ||
if (!isset($this->presets[$offset])) { | ||
$this->presets[$offset] = new Preset($offset, $this->apiCall); | ||
} | ||
|
||
return $this->presets[$offset]; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function offsetSet($offset, $value): void | ||
{ | ||
$this->presets[$offset] = $value; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function offsetUnset($offset): void | ||
{ | ||
unset($this->presets[$offset]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters