Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit 203d641

Browse files
authored
Merge pull request #33 from yoanbernabeu/features/edit-record
Features/edit record
2 parents a272cb5 + 504ddb9 commit 203d641

File tree

6 files changed

+88
-25
lines changed

6 files changed

+88
-25
lines changed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Airtable Client Bundle (Work In Progress)
1+
Airtable Client Bundle
22
==================
33

44
[![Static code analysis](https://github.com/yoanbernabeu/Airtable-Client-Bundle/actions/workflows/code_analysis.yml/badge.svg)](https://github.com/yoanbernabeu/Airtable-Client-Bundle/actions/workflows/code_analysis.yml)
@@ -116,6 +116,36 @@ class FooController
116116
// ...
117117
}
118118
```
119+
### Edit
120+
121+
```php
122+
123+
use Yoanbernabeu\AirtableClientBundle\AirtableClientInterface;
124+
125+
class Foo
126+
{
127+
public int $id;
128+
public string $bar;
129+
}
130+
131+
class FooController
132+
{
133+
public function bar(AirtableClientInterface $airtableClient)
134+
{
135+
$airtableClient->update(
136+
'tableName',
137+
'recordId',
138+
[
139+
'id' => 1,
140+
'bar' => 'lorem ipsum',
141+
Foo::class
142+
]
143+
);
144+
}
145+
146+
// ...
147+
}
148+
```
119149

120150
### Create Form
121151

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
"require-dev": {
3333
"ext-json": "*",
3434
"friendsofphp/php-cs-fixer": "^3.0",
35-
"phpstan/phpstan": "^0.12",
36-
"phpstan/phpstan-beberlei-assert": "^0.12",
37-
"phpstan/phpstan-deprecation-rules": "^0.12",
38-
"phpstan/phpstan-phpunit": "^0.12",
39-
"phpstan/phpstan-strict-rules": "^0.12",
35+
"phpstan/phpstan": "^1.0",
36+
"phpstan/phpstan-beberlei-assert": "^1.0",
37+
"phpstan/phpstan-deprecation-rules": "^1.0",
38+
"phpstan/phpstan-phpunit": "^1.0",
39+
"phpstan/phpstan-strict-rules": "^1.0",
4040
"symfony/framework-bundle": "^5.0|^6.0",
4141
"symfony/phpunit-bridge": "^5.0|^6.0"
4242
},

src/AirtableClient.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,28 +120,19 @@ public function findLast(string $table, $field, ?string $dataClass = null): ?Air
120120
*/
121121
public function add(string $table, array $fields, ?string $dataClass = null): ?AirtableRecord
122122
{
123-
$url = sprintf(
124-
'%s',
125-
$table
126-
);
127-
128-
$response = $this->airtableTransport->request(
129-
'POST',
130-
$url,
131-
['json' => [
132-
'fields' => $fields, ],
133-
]
134-
);
123+
$url = sprintf('%s', $table);
135124

136-
$recordData = $response->toArray();
137-
138-
if ([] === $recordData) {
139-
return null;
140-
}
125+
return $this->createOrUpdateRecord('POST', $url, $fields, $dataClass);
126+
}
141127

142-
$recordData = $this->createRecordFromResponse($dataClass, $recordData);
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
public function update(string $table, string $recordId, array $fields, ?string $dataClass = null): ?AirtableRecord
132+
{
133+
$url = sprintf('%s/%s', $table, $recordId);
143134

144-
return AirtableRecord::createFromRecord($recordData);
135+
return $this->createOrUpdateRecord('PATCH', $url, $fields, $dataClass);
145136
}
146137

147138
/**
@@ -221,4 +212,33 @@ private function createRecordFromResponse(?string $dataClass, array $recordData)
221212

222213
return $recordData;
223214
}
215+
216+
/**
217+
* @throws Exception\MissingRecordDataException
218+
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
219+
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
220+
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
221+
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
222+
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
223+
*/
224+
private function createOrUpdateRecord(string $method, string $url, array $fields, ?string $dataClass): ?AirtableRecord
225+
{
226+
$response = $this->airtableTransport->request(
227+
$method,
228+
$url,
229+
[
230+
'json' => ['fields' => $fields],
231+
]
232+
);
233+
234+
$recordData = $response->toArray();
235+
236+
if ([] === $recordData) {
237+
return null;
238+
}
239+
240+
$recordData = $this->createRecordFromResponse($dataClass, $recordData);
241+
242+
return AirtableRecord::createFromRecord($recordData);
243+
}
224244
}

src/AirtableClientInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public function findLast(string $table, $field, ?string $dataClass = null): ?Air
6767
*/
6868
public function add(string $table, array $fields, ?string $dataClass = null): ?AirtableRecord;
6969

70+
/**
71+
* Update a record and return the record.
72+
*
73+
* @param string $table Table name
74+
* @param string $recordId Record Id of the element
75+
* @param array $fields Table fields
76+
* @param string|null $dataClass The name of the class which will hold fields data
77+
*/
78+
public function update(string $table, string $recordId, array $fields, ?string $dataClass = null): ?AirtableRecord;
79+
7080
/**
7181
* Create form from an array of fields.
7282
*

src/Resources/config/services.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
declare(strict_types=1);
44

55
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
6+
67
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
78
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
9+
810
use Yoanbernabeu\AirtableClientBundle\AirtableClient;
911
use Yoanbernabeu\AirtableClientBundle\AirtableClientInterface;
1012
use Yoanbernabeu\AirtableClientBundle\AirtableTransport;

tests/Unit/AirtableRecordTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AirtableRecordTest extends TestCase
1515
{
1616
/**
1717
* @test
18+
*
1819
* @dataProvider invalidRecordArrayProvider
1920
*/
2021
public function createFromRecordWillThrowIfADataIsMissingInArray(array $recordData, string $message)

0 commit comments

Comments
 (0)