1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Project airtable-sdk-php |
4
|
|
|
* File: Table.php |
5
|
|
|
* Created by: tpojka |
6
|
|
|
* On: 26/03/2020 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Beachcasts\Airtable; |
12
|
|
|
|
13
|
|
|
use Beachcasts\Airtable\Request\TableRequest as TableRequest; |
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Table |
19
|
|
|
* @package Beachcasts\Airtable |
20
|
|
|
*/ |
21
|
|
|
class Table |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string|null $tableName |
25
|
|
|
*/ |
26
|
|
|
protected $tableName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Client $client |
30
|
|
|
*/ |
31
|
|
|
protected $client; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Table constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $tableName |
37
|
|
|
*/ |
38
|
2 |
|
public function __construct(string $tableName) |
39
|
|
|
{ |
40
|
2 |
|
$this->tableName = $tableName; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Client $client |
45
|
|
|
*/ |
46
|
1 |
|
public function setClient(Client $client): void |
47
|
|
|
{ |
48
|
1 |
|
$this->client = $client; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
2 |
|
public function getName(): ?string |
55
|
|
|
{ |
56
|
2 |
|
return $this->tableName; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
* Params could include the following: |
62
|
|
|
* fields = ['column_1_name', 'column_2_name', 'column_3_name] |
63
|
|
|
* filterByFormula = "NOT({Headline} = '')" |
64
|
|
|
* maxRecords = 100 |
65
|
|
|
* pageSize = 100 |
66
|
|
|
* sort = [{field: "Headline", direction: "desc"}] |
67
|
|
|
* view = "view_name" |
68
|
|
|
* |
69
|
|
|
* @param array $params |
70
|
|
|
* @return ResponseInterface |
71
|
|
|
*/ |
72
|
|
|
public function list(array $params) |
73
|
|
|
{ |
74
|
|
|
return $this->client->send( |
75
|
|
|
TableRequest::listRecords($this->getName(), $params) |
|
|
|
|
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $records |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function create(array $records) |
84
|
|
|
{ |
85
|
|
|
return $this->client->send( |
86
|
|
|
TableRequest::createRecords($this->getName(), $records) |
|
|
|
|
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $id |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
public function read(string $id) |
95
|
|
|
{ |
96
|
|
|
return $this->client->send( |
97
|
|
|
TableRequest::readRecords($this->tableName, $id) |
|
|
|
|
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $records |
103
|
|
|
* @param string $type accepts PUT to replace or PATCH to update records |
104
|
|
|
* @return mixed |
105
|
|
|
* @throws \Exception |
106
|
|
|
* @todo split out to a replace method for PUT |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
|
|
public function update(array $records, $type = 'PATCH') |
110
|
|
|
{ |
111
|
|
|
return $this->client->send( |
112
|
|
|
TableRequest::updateRecords($this->getName(), $records, $type) |
|
|
|
|
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $records |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
|
|
public function delete(array $records) |
121
|
|
|
{ |
122
|
|
|
return $this->client->send( |
123
|
|
|
TableRequest::deleteRecords($this->getName(), $records) |
|
|
|
|
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|