Skip to content

Commit 261ed55

Browse files
authored
Typecasting conversion (#22)
* Implemented typecast option * updated readme * Fix * StyleCI fix * Removed Extra Condition
1 parent 482ce83 commit 261ed55

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ Define airtables account information in .env:
2727
AIRTABLE_KEY=
2828
AIRTABLE_BASE=
2929
AIRTABLE_TABLE=
30+
AIRTABLE_TYPECAST=false
3031
```
3132

3233
* `AIRTABLE_KEY` can be retrieved here: https://airtable.com/account
3334
* `AIRTABLE_BASE` can be found here: https://airtable.com/api, select base then copy from URL: `https://airtable.com/[Base Is Here]/api/docs#curl/introduction`
3435
* `AIRTABLE_TABLE` can be found in the docs for the appropriate base, this is not case senstive. IE: `tasks`
36+
* `AIRTABLE_TYPECAST` set this to true to allow automatic casting.
3537

3638
## Example Config
3739

config/config.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
| This value can be found in your Airtable account page:
1111
| https://airtable.com/account
1212
|
13-
*/
13+
*/
1414
'key' => env('AIRTABLE_KEY'),
1515

1616
/*
@@ -22,7 +22,7 @@
2222
| https://airtable.com/api
2323
| https://airtable.com/[BASE_ID]/api/docs#curl/introduction
2424
|
25-
*/
25+
*/
2626
'base' => env('AIRTABLE_BASE'),
2727

2828
/*
@@ -36,7 +36,7 @@
3636
| Example:
3737
| Each record in the `Tasks` contains the following fields
3838
|
39-
*/
39+
*/
4040
'default' => 'default',
4141

4242
'tables' => [
@@ -49,4 +49,6 @@
4949

5050
'log_http' => env('AIRTABLE_LOG_HTTP', false),
5151
'log_http_format' => env('AIRTABLE_LOG_HTTP_FORMAT', '{request} >>> {res_body}'),
52+
53+
'typecast' => env('AIRTABLE_TYPECAST', false),
5254
];

src/AirtableManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ protected function createAirtable($table)
119119
$httpLogFormat = null;
120120
}
121121

122-
$client = new AirtableApiClient($base, $table, $access_token, $httpLogFormat);
122+
$airtableTypeCast = $this->app['config']['airtable.typecast'];
123+
124+
$client = new AirtableApiClient($base, $table, $access_token, $httpLogFormat, null, $airtableTypeCast);
123125

124126
return new Airtable($client, $table);
125127
}

src/Api/AirtableApiClient.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ class AirtableApiClient implements ApiClient
99
{
1010
private $client;
1111

12+
private $typecast;
1213
private $base;
1314
private $table;
1415

1516
private $filters = [];
1617
private $pageSize = 100;
1718
private $maxRecords = 100;
1819

19-
public function __construct($base, $table, $access_token, $httpLogFormat = null, Client $client = null)
20+
public function __construct($base, $table, $access_token, $httpLogFormat = null, Client $client = null, $typecast = false)
2021
{
2122
$this->base = $base;
2223
$this->table = $table;
24+
$this->typecast = $typecast;
2325

2426
$stack = \GuzzleHttp\HandlerStack::create();
2527

@@ -49,7 +51,7 @@ private function buildClient($access_token, $stack)
4951

5052
public function addFilter($column, $operation, $value)
5153
{
52-
$this->filters [] = "{{$column}}{$operation}\"{$value}\"";
54+
$this->filters[] = "{{$column}}{$operation}\"{$value}\"";
5355

5456
return $this;
5557
}
@@ -107,7 +109,7 @@ public function post($contents = null)
107109
{
108110
$url = $this->getEndpointUrl();
109111

110-
$params = ['json' => ['fields' => (object) $contents]];
112+
$params = ['json' => ['fields' => (object) $contents, 'typecast' => $this->typecast]];
111113

112114
return $this->decodeResponse($this->client->post($url, $params));
113115
}
@@ -116,7 +118,7 @@ public function put(string $id, $contents = null)
116118
{
117119
$url = $this->getEndpointUrl($id);
118120

119-
$params = ['json' => ['fields' => (object) $contents]];
121+
$params = ['json' => ['fields' => (object) $contents, 'typecast' => $this->typecast]];
120122

121123
return $this->decodeResponse($this->client->put($url, $params));
122124
}
@@ -125,7 +127,7 @@ public function patch(string $id, $contents = null)
125127
{
126128
$url = $this->getEndpointUrl($id);
127129

128-
$params = ['json' => ['fields' => (object) $contents]];
130+
$params = ['json' => ['fields' => (object) $contents, 'typecast' => $this->typecast]];
129131

130132
return $this->decodeResponse($this->client->patch($url, $params));
131133
}

0 commit comments

Comments
 (0)