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
|
|
|
// if (!empty($params)) |
75
|
|
|
// $queryString = http_build_query($params); |
76
|
|
|
// |
77
|
|
|
// $url = $this->tableName . '?' . $queryString; |
78
|
|
|
|
79
|
|
|
// return $this->client->request('GET', $url); |
80
|
|
|
|
81
|
|
|
return $this->client->send( |
82
|
|
|
TableRequest::listRecords($this->getName(), $params) |
|
|
|
|
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $records |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
public function create(array $records) |
91
|
|
|
{ |
92
|
|
|
return $this->client->send( |
93
|
|
|
TableRequest::createRecords($this->getName(), $records) |
|
|
|
|
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $id |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function read(string $id) |
102
|
|
|
{ |
103
|
|
|
return $this->client->send( |
104
|
|
|
TableRequest::readRecords($this->tableName, $id) |
|
|
|
|
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $records |
110
|
|
|
* @param string $type accepts PUT to replace or PATCH to update records |
111
|
|
|
* @return mixed |
112
|
|
|
* @throws \Exception |
113
|
|
|
* @todo split out to a replace method for PUT |
114
|
|
|
* |
115
|
|
|
*/ |
116
|
|
|
public function update(array $records, $type = 'PATCH') |
117
|
|
|
{ |
118
|
|
|
return $this->client->send( |
119
|
|
|
TableRequest::updateRecords($this->getName(), $records, $type) |
|
|
|
|
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $recordId |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
public function delete(string $recordId) |
128
|
|
|
{ |
129
|
|
|
return $this->client->send( |
130
|
|
|
TableRequest::deleteRecords($this->getName(), $recordId) |
|
|
|
|
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|