Skip to content

Commit 2f601a4

Browse files
committed
feat(curation): add curation set classes
1 parent 4e2c8c4 commit 2f601a4

File tree

4 files changed

+367
-0
lines changed

4 files changed

+367
-0
lines changed

src/CurationSet.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class CurationSet
10+
*
11+
* @package \Typesense
12+
*/
13+
class CurationSet
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private string $curationSetName;
19+
20+
/**
21+
* @var ApiCall
22+
*/
23+
private ApiCall $apiCall;
24+
25+
/**
26+
* @var CurationSetItems
27+
*/
28+
private CurationSetItems $items;
29+
30+
/**
31+
* CurationSet constructor.
32+
*
33+
* @param string $curationSetName
34+
* @param ApiCall $apiCall
35+
*/
36+
public function __construct(string $curationSetName, ApiCall $apiCall)
37+
{
38+
$this->curationSetName = $curationSetName;
39+
$this->apiCall = $apiCall;
40+
$this->items = new CurationSetItems($curationSetName, $apiCall);
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
private function endPointPath(): string
47+
{
48+
return sprintf(
49+
'%s/%s',
50+
CurationSets::RESOURCE_PATH,
51+
encodeURIComponent($this->curationSetName)
52+
);
53+
}
54+
55+
/**
56+
* @param array $params
57+
*
58+
* @return array
59+
* @throws TypesenseClientError|HttpClientException
60+
*/
61+
public function upsert(array $params): array
62+
{
63+
return $this->apiCall->put($this->endPointPath(), $params);
64+
}
65+
66+
/**
67+
* @return array
68+
* @throws TypesenseClientError|HttpClientException
69+
*/
70+
public function retrieve(): array
71+
{
72+
return $this->apiCall->get($this->endPointPath(), []);
73+
}
74+
75+
/**
76+
* @return array
77+
* @throws TypesenseClientError|HttpClientException
78+
*/
79+
public function delete(): array
80+
{
81+
return $this->apiCall->delete($this->endPointPath());
82+
}
83+
84+
/**
85+
* @return CurationSetItems
86+
*/
87+
public function getItems(): CurationSetItems
88+
{
89+
return $this->items;
90+
}
91+
}

src/CurationSetItem.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class CurationSetItem
10+
*
11+
* @package \Typesense
12+
*/
13+
class CurationSetItem
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private string $curationSetName;
19+
20+
/**
21+
* @var string
22+
*/
23+
private string $itemId;
24+
25+
/**
26+
* @var ApiCall
27+
*/
28+
private ApiCall $apiCall;
29+
30+
/**
31+
* CurationSetItem constructor.
32+
*
33+
* @param string $curationSetName
34+
* @param string $itemId
35+
* @param ApiCall $apiCall
36+
*/
37+
public function __construct(string $curationSetName, string $itemId, ApiCall $apiCall)
38+
{
39+
$this->curationSetName = $curationSetName;
40+
$this->itemId = $itemId;
41+
$this->apiCall = $apiCall;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
private function endPointPath(): string
48+
{
49+
return sprintf(
50+
'%s/%s/items/%s',
51+
CurationSets::RESOURCE_PATH,
52+
encodeURIComponent($this->curationSetName),
53+
encodeURIComponent($this->itemId)
54+
);
55+
}
56+
57+
/**
58+
* @return array
59+
* @throws TypesenseClientError|HttpClientException
60+
*/
61+
public function retrieve(): array
62+
{
63+
return $this->apiCall->get($this->endPointPath(), []);
64+
}
65+
66+
/**
67+
* @param array $params
68+
*
69+
* @return array
70+
* @throws TypesenseClientError|HttpClientException
71+
*/
72+
public function upsert(array $params): array
73+
{
74+
return $this->apiCall->put($this->endPointPath(), $params);
75+
}
76+
77+
/**
78+
* @return array
79+
* @throws TypesenseClientError|HttpClientException
80+
*/
81+
public function delete(): array
82+
{
83+
return $this->apiCall->delete($this->endPointPath());
84+
}
85+
}

src/CurationSetItems.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class CurationSetItems
10+
*
11+
* @package \Typesense
12+
*/
13+
class CurationSetItems implements \ArrayAccess
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private string $curationSetName;
19+
20+
/**
21+
* @var ApiCall
22+
*/
23+
private ApiCall $apiCall;
24+
25+
/**
26+
* @var array
27+
*/
28+
private array $items = [];
29+
30+
/**
31+
* CurationSetItems constructor.
32+
*
33+
* @param string $curationSetName
34+
* @param ApiCall $apiCall
35+
*/
36+
public function __construct(string $curationSetName, ApiCall $apiCall)
37+
{
38+
$this->curationSetName = $curationSetName;
39+
$this->apiCall = $apiCall;
40+
}
41+
42+
/**
43+
* @return string
44+
*/
45+
private function endPointPath(): string
46+
{
47+
return sprintf(
48+
'%s/%s/items',
49+
CurationSets::RESOURCE_PATH,
50+
encodeURIComponent($this->curationSetName)
51+
);
52+
}
53+
54+
/**
55+
* @return array
56+
* @throws TypesenseClientError|HttpClientException
57+
*/
58+
public function retrieve(): array
59+
{
60+
return $this->apiCall->get($this->endPointPath(), []);
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function offsetExists($itemId): bool
67+
{
68+
return isset($this->items[$itemId]);
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
public function offsetGet($itemId): CurationSetItem
75+
{
76+
if (!isset($this->items[$itemId])) {
77+
$this->items[$itemId] = new CurationSetItem($this->curationSetName, $itemId, $this->apiCall);
78+
}
79+
80+
return $this->items[$itemId];
81+
}
82+
83+
/**
84+
* @inheritDoc
85+
*/
86+
public function offsetSet($itemId, $value): void
87+
{
88+
$this->items[$itemId] = $value;
89+
}
90+
91+
/**
92+
* @inheritDoc
93+
*/
94+
public function offsetUnset($itemId): void
95+
{
96+
unset($this->items[$itemId]);
97+
}
98+
}

src/CurationSets.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Typesense;
4+
5+
use Http\Client\Exception as HttpClientException;
6+
use Typesense\Exceptions\TypesenseClientError;
7+
8+
/**
9+
* Class CurationSets
10+
*
11+
* @package \Typesense
12+
*/
13+
class CurationSets implements \ArrayAccess
14+
{
15+
public const RESOURCE_PATH = '/curation_sets';
16+
17+
/**
18+
* @var ApiCall
19+
*/
20+
private ApiCall $apiCall;
21+
22+
/**
23+
* @var array
24+
*/
25+
private array $curationSets = [];
26+
27+
/**
28+
* CurationSets constructor.
29+
*
30+
* @param ApiCall $apiCall
31+
*/
32+
public function __construct(ApiCall $apiCall)
33+
{
34+
$this->apiCall = $apiCall;
35+
}
36+
37+
/**
38+
* @param string $curationSetName
39+
* @param array $config
40+
*
41+
* @return array
42+
* @throws TypesenseClientError|HttpClientException
43+
*/
44+
public function upsert(string $curationSetName, array $config): array
45+
{
46+
return $this->apiCall->put(sprintf('%s/%s', static::RESOURCE_PATH, encodeURIComponent($curationSetName)), $config);
47+
}
48+
49+
/**
50+
* @return array
51+
* @throws TypesenseClientError|HttpClientException
52+
*/
53+
public function retrieve(): array
54+
{
55+
return $this->apiCall->get(static::RESOURCE_PATH, []);
56+
}
57+
58+
/**
59+
* @inheritDoc
60+
*/
61+
public function offsetExists($curationSetName): bool
62+
{
63+
return isset($this->curationSets[$curationSetName]);
64+
}
65+
66+
/**
67+
* @inheritDoc
68+
*/
69+
public function offsetGet($curationSetName): CurationSet
70+
{
71+
if (!isset($this->curationSets[$curationSetName])) {
72+
$this->curationSets[$curationSetName] = new CurationSet($curationSetName, $this->apiCall);
73+
}
74+
75+
return $this->curationSets[$curationSetName];
76+
}
77+
78+
/**
79+
* @inheritDoc
80+
*/
81+
public function offsetSet($curationSetName, $value): void
82+
{
83+
$this->curationSets[$curationSetName] = $value;
84+
}
85+
86+
/**
87+
* @inheritDoc
88+
*/
89+
public function offsetUnset($curationSetName): void
90+
{
91+
unset($this->curationSets[$curationSetName]);
92+
}
93+
}

0 commit comments

Comments
 (0)