Skip to content

Commit dbdc7c4

Browse files
yassinebourakbaYassine Bourakba
andauthored
Select specific fields in result (#30)
* Add setFields method to ApiClient * Support fields in query params and refactor query params getter * Add `select` method in Airtable class * Update `get` method to support array of fields * code style * Fix code style * code style Co-authored-by: Yassine Bourakba <[email protected]>
1 parent d69ff96 commit dbdc7c4

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Airtable.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ public function destroy(string $id)
3636
return $this->api->delete($id);
3737
}
3838

39-
public function get()
39+
public function get(array $fields = [])
4040
{
41+
if ($fields) {
42+
$this->select($fields);
43+
}
44+
4145
return $this->toCollection($this->api->get());
4246
}
4347

@@ -109,6 +113,13 @@ public function updateOrCreate(array $idData, array $updateData = [])
109113
return $this->create($data);
110114
}
111115

116+
public function select(array $fields = [])
117+
{
118+
$this->api->setFields($fields);
119+
120+
return $this;
121+
}
122+
112123
private function toCollection($object)
113124
{
114125
return isset($object['records']) ? collect($object['records']) : $object;

src/Api/AirtableApiClient.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AirtableApiClient implements ApiClient
1414
private $table;
1515

1616
private $filters = [];
17+
private $fields = [];
1718
private $pageSize = 100;
1819
private $maxRecords = 100;
1920

@@ -170,6 +171,13 @@ public function decodeResponse($response)
170171
return json_decode($body, true);
171172
}
172173

174+
public function setFields(?array $fields)
175+
{
176+
$this->fields = $fields;
177+
178+
return $this;
179+
}
180+
173181
protected function getEndpointUrl(?string $id = null): string
174182
{
175183
if ($id) {
@@ -189,12 +197,25 @@ protected function getEndpointUrl(?string $id = null): string
189197
$this->table,
190198
], $url);
191199

192-
if ($this->filters) {
193-
$url .= '?'.http_build_query([
194-
'filterByFormula' => 'AND('.implode(',', $this->filters).')',
195-
]);
200+
if ($query_params = $this->getQueryParams()) {
201+
$url .= '?'.http_build_query($query_params);
196202
}
197203

198204
return $url;
199205
}
206+
207+
protected function getQueryParams(): array
208+
{
209+
$query_params = [];
210+
211+
if ($this->filters) {
212+
$query_params['filterByFormula'] = 'AND('.implode(',', $this->filters).')';
213+
}
214+
215+
if ($this->fields) {
216+
$query_params['fields'] = $this->fields;
217+
}
218+
219+
return $query_params;
220+
}
200221
}

0 commit comments

Comments
 (0)