Skip to content

Commit 4e41737

Browse files
Merge pull request #57 from AbdullahFaqeir/master
- Added exists() method to Collection.
2 parents 7f72847 + f30565f commit 4e41737

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

examples/collection_operations.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
);
7777
echo "--------Create Collection-------\n";
7878
echo "\n";
79+
echo "--------Check if Collection exists-------\n";
80+
print_r($client->collections['books']->exists());
81+
echo "--------Check if Collection exists-------\n";
82+
echo "\n";
7983
echo "--------Retrieve Collection-------\n";
8084
print_r($client->collections['books']->retrieve());
8185
echo "--------Retrieve Collection-------\n";

src/Collection.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
class Collection
1616
{
17-
1817
/**
1918
* @var string
2019
*/
@@ -40,10 +39,15 @@ class Collection
4039
*/
4140
public Synonyms $synonyms;
4241

42+
/**
43+
* @var bool|null
44+
*/
45+
private ?bool $exists = null;
46+
4347
/**
4448
* Collection constructor.
4549
*
46-
* @param string $name
50+
* @param string $name
4751
* @param ApiCall $apiCall
4852
*/
4953
public function __construct(string $name, ApiCall $apiCall)
@@ -87,6 +91,23 @@ public function getSynonyms(): Synonyms
8791
return $this->synonyms;
8892
}
8993

94+
/**
95+
* @return bool|null
96+
*/
97+
public function exists(): ?bool
98+
{
99+
if ($this->exists === null) {
100+
try {
101+
$this->retrieve();
102+
$this->exists = true;
103+
} catch (TypesenseClientError | HttpClientException $e) {
104+
$this->exists = false;
105+
}
106+
}
107+
108+
return $this->exists;
109+
}
110+
90111
/**
91112
* @return array
92113
* @throws TypesenseClientError|HttpClientException

0 commit comments

Comments
 (0)