Repository navigation
Expand file tree
/
Copy pathCollections.php
More file actions
123 lines (108 loc) · 3 KB
/
Copy pathCollections.php
File metadata and controls
123 lines (108 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Typesense;
use Http\Client\Exception as HttpClientException;
use Typesense\Exceptions\TypesenseClientError;
/**
* Class Collections
*
* @package \Typesense
* @date 4/5/20
* @author Abdullah Al-Faqeir <abdullah@devloops.net>
*/
class Collections implements \ArrayAccess
{
public const RESOURCE_PATH = '/collections';
/**
* @var ApiCall
*/
private ApiCall $apiCall;
/**
* @var array
*/
private array $typesenseCollections = [];
/**
* Collections constructor.
*
* @param ApiCall $apiCall
*/
public function __construct(ApiCall $apiCall)
{
$this->apiCall = $apiCall;
}
/**
* @param $collectionName
*
* @return mixed
*/
public function __get($collectionName)
{
if (!isset($this->typesenseCollections[$collectionName])) {
$this->typesenseCollections[$collectionName] = new Collection($collectionName, $this->apiCall);
}
return $this->typesenseCollections[$collectionName];
}
/**
* When a collection is created, we give it a name and describe the fields that will be indexed from the documents added to the collection.
*
* @example
* $client->collections->create(['name' => 'products', 'fields' => [['name' => 'title', 'type' => 'string']]])
*
* @see https://typesense.org/docs/latest/api/collections.html#create-a-collection
*
* @param array $schema
* @param array $options
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function create(array $schema, array $options = []): array
{
return $this->apiCall->post(static::RESOURCE_PATH, $schema, true, $options);
}
/**
* Returns a summary of all your collections. The collections are returned sorted by creation date, with the most recent collections appearing first.
*
* @example
* $client->collections->retrieve()
*
* @see https://typesense.org/docs/latest/api/collections.html#list-all-collections
*
* @return array
* @throws TypesenseClientError|HttpClientException
*/
public function retrieve(): array
{
return $this->apiCall->get(static::RESOURCE_PATH, []);
}
/**
* @inheritDoc
*/
public function offsetExists($offset): bool
{
return isset($this->typesenseCollections[$offset]);
}
/**
* @inheritDoc
*/
public function offsetGet($offset): Collection
{
if (!isset($this->typesenseCollections[$offset])) {
$this->typesenseCollections[$offset] = new Collection($offset, $this->apiCall);
}
return $this->typesenseCollections[$offset];
}
/**
* @inheritDoc
*/
public function offsetSet($offset, $value): void
{
$this->typesenseCollections[$offset] = $value;
}
/**
* @inheritDoc
*/
public function offsetUnset($offset): void
{
unset($this->typesenseCollections[$offset]);
}
}