Skip to content

Commit d7f5fc7

Browse files
authored
Add sorting support, fixes #41 (#43)
* Add sort support, fixes #41 * Docs
1 parent 13e4057 commit d7f5fc7

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ Airtable::where('id', '5')->get();
9191
Airtable::where('id', '>', '5')->get();
9292
```
9393

94+
#### Sorting records
95+
96+
- First argument is the column name
97+
- Second argument is the sort direction: `asc` (default) or `desc`
98+
99+
``` php
100+
Airtable::orderBy('id')->get();
101+
Airtable::orderBy('created_at', 'desc')->get();
102+
```
103+
You can sort by multiple fields by calling `orderBy` more than once (a single call with array syntax is not supported):
104+
```php
105+
Airtable::orderBy('id')->orderBy('created_at', 'desc')->get();
106+
```
107+
94108
#### First or Create
95109
- First argument will be used for finding existing
96110
- Second argument is additional data to save if no results are found and we are creating (will not be saved used if item already exists)

src/Airtable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ public function where($column, $operator, $value = null)
106106
return $this;
107107
}
108108

109+
public function orderBy(string $column, string $direction = 'asc')
110+
{
111+
$this->api->addSort($column, $direction);
112+
113+
return $this;
114+
}
115+
109116
public function firstOrCreate(array $idData, array $createData = [])
110117
{
111118
foreach ($idData as $key => $value) {

src/Api/AirtableApiClient.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class AirtableApiClient implements ApiClient
1616

1717
private $filters = [];
1818
private $fields = [];
19+
private $sorts = [];
1920
private $offset = false;
2021
private $pageSize = 100;
2122
private $maxRecords = 100;
@@ -60,6 +61,17 @@ public function addFilter($column, $operation, $value)
6061
return $this;
6162
}
6263

64+
public function addSort(string $column, string $direction = 'asc')
65+
{
66+
if ($direction === 'desc') {
67+
$this->sorts[] = ['field' => $column, 'direction' => $direction];
68+
} else {
69+
$this->sorts[] = ['field' => $column];
70+
}
71+
72+
return $this;
73+
}
74+
6375
public function setTable($table)
6476
{
6577
$this->table = $table;
@@ -231,6 +243,10 @@ protected function getQueryParams(): array
231243
$query_params['offset'] = $this->offset;
232244
}
233245

246+
if ($this->sorts) {
247+
$query_params['sort'] = $this->sorts;
248+
}
249+
234250
return $query_params;
235251
}
236252
}

tests/ClientTest.php

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public function it_can_be_instantiated()
2020
public function it_can_post()
2121
{
2222
$expectedResponse = [
23-
'id' => 'randomlygenerated',
24-
'fields' => ['Company Name' => 'Tapp Network'],
23+
'id' => 'randomlygenerated',
24+
'fields' => ['Company Name' => 'Tapp Network'],
2525
'createdTime' => 'timestamp',
2626
];
2727

@@ -32,7 +32,7 @@ public function it_can_post()
3232
'/v0/test_base/companies',
3333
[
3434
'json' => [
35-
'fields' => (object) $postData,
35+
'fields' => (object)$postData,
3636
],
3737
]
3838
);
@@ -75,6 +75,78 @@ public function it_can_search()
7575
$this->assertEquals($expectedResponse['fields'], $first['fields']);
7676
}
7777

78+
/** @test */
79+
public function it_can_sort()
80+
{
81+
//Ascending sort
82+
$expectedResponseAsc = [
83+
[
84+
'id' => 0,
85+
'fields' => ['Company Name' => 'A Network'],
86+
'createdTime' => 'timestamp',
87+
],
88+
[
89+
'id' => 1,
90+
'fields' => ['Company Name' => 'B Network'],
91+
'createdTime' => 'timestamp',
92+
],
93+
];
94+
95+
$mockGuzzle = $this->mock_guzzle_request(
96+
json_encode($expectedResponseAsc),
97+
'/v0/test_base/companies',
98+
[
99+
'json' => [
100+
'sort' => '[{field:"Company Name",direction:"asc"}]',
101+
],
102+
]
103+
);
104+
105+
$client = $this->build_client($mockGuzzle);
106+
107+
$actualResponse = $client->setTable('companies')
108+
->addSort('Company Name', 'asc')
109+
->get();
110+
111+
$first = $actualResponse['records'][0];
112+
113+
$this->assertEquals($expectedResponseAsc['fields'], $first['fields']);
114+
115+
//Descending sort
116+
$expectedResponseDesc = [
117+
[
118+
'id' => 1,
119+
'fields' => ['Company Name' => 'B Network'],
120+
'createdTime' => 'timestamp',
121+
],
122+
[
123+
'id' => 0,
124+
'fields' => ['Company Name' => 'A Network'],
125+
'createdTime' => 'timestamp',
126+
],
127+
];
128+
129+
$mockGuzzle = $this->mock_guzzle_request(
130+
json_encode($expectedResponseDesc),
131+
'/v0/test_base/companies',
132+
[
133+
'json' => [
134+
'sort' => '[{field:"Company Name",direction:"desc"}]',
135+
],
136+
]
137+
);
138+
139+
$client = $this->build_client($mockGuzzle);
140+
141+
$actualResponse = $client->setTable('companies')
142+
->addSort('Company Name', 'desc')
143+
->get();
144+
145+
$first = $actualResponse['records'][0];
146+
147+
$this->assertEquals($expectedResponseDesc['fields'], $first['fields']);
148+
}
149+
78150
private function build_client($mockGuzzle = null)
79151
{
80152
if (env('LOG_HTTP')) {

0 commit comments

Comments
 (0)