66use Typesense \Exceptions \TypesenseClientError ;
77
88/**
9- * Class Document
9+ * Class Presets
1010 *
1111 * @package \Typesense
1212 * @date 4/5/20
1313 * @author Abdullah Al-Faqeir <abdullah@devloops.net>
1414 */
15- class Presets
15+ class Presets implements \ArrayAccess
1616{
1717 /**
1818 * @var ApiCall
@@ -24,7 +24,12 @@ class Presets
2424 public const MULTI_SEARCH_PATH = '/multi_search ' ;
2525
2626 /**
27- * Document constructor.
27+ * @var array
28+ */
29+ private array $ presets = [];
30+
31+ /**
32+ * Presets constructor.
2833 *
2934 * @param ApiCall $apiCall
3035 */
@@ -49,37 +54,24 @@ public function searchWithPreset($presetName)
4954 * @throws HttpClientException
5055 * @throws TypesenseClientError
5156 */
52- public function get ()
57+ public function retrieve ()
5358 {
5459 return $ this ->apiCall ->get (static ::PRESETS_PATH , []);
5560 }
5661
5762 /**
63+ * @param string $presetName
5864 * @param array $options
5965 *
6066 * @return array
6167 * @throws HttpClientException
6268 * @throws TypesenseClientError
6369 */
64- public function put ( array $ options = [] )
70+ public function upsert ( string $ presetName , array $ presetsData )
6571 {
66- $ presetName = $ options ['preset_name ' ];
67- $ presetsData = $ options ['preset_data ' ];
68-
6972 return $ this ->apiCall ->put ($ this ->endpointPath ($ presetName ), $ presetsData );
7073 }
7174
72- /**
73- * @param $presetName
74- * @return array
75- * @throws HttpClientException
76- * @throws TypesenseClientError
77- */
78- public function delete ($ presetName )
79- {
80- return $ this ->apiCall ->delete ($ this ->endpointPath ($ presetName ));
81- }
82-
8375 /**
8476 * @param $presetsName
8577 * @return string
@@ -104,4 +96,57 @@ private function multiSearchEndpointPath()
10496 static ::MULTI_SEARCH_PATH
10597 );
10698 }
99+
100+ /**
101+ * @param $presetName
102+ *
103+ * @return mixed
104+ */
105+ public function __get ($ presetName )
106+ {
107+ if (isset ($ this ->{$ presetName })) {
108+ return $ this ->{$ presetName };
109+ }
110+ if (!isset ($ this ->presets [$ presetName ])) {
111+ $ this ->presets [$ presetName ] = new Preset ($ presetName , $ this ->apiCall );
112+ }
113+
114+ return $ this ->presets [$ presetName ];
115+ }
116+
117+ /**
118+ * @inheritDoc
119+ */
120+ public function offsetExists ($ offset ): bool
121+ {
122+ return isset ($ this ->presets [$ offset ]);
123+ }
124+
125+ /**
126+ * @inheritDoc
127+ */
128+ public function offsetGet ($ offset ): Preset
129+ {
130+ if (!isset ($ this ->presets [$ offset ])) {
131+ $ this ->presets [$ offset ] = new Preset ($ offset , $ this ->apiCall );
132+ }
133+
134+ return $ this ->presets [$ offset ];
135+ }
136+
137+ /**
138+ * @inheritDoc
139+ */
140+ public function offsetSet ($ offset , $ value ): void
141+ {
142+ $ this ->presets [$ offset ] = $ value ;
143+ }
144+
145+ /**
146+ * @inheritDoc
147+ */
148+ public function offsetUnset ($ offset ): void
149+ {
150+ unset($ this ->presets [$ offset ]);
151+ }
107152}
0 commit comments