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
|
|
|
* @var string $viewName |
35
|
|
|
*/ |
36
|
|
|
protected $viewName; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Table constructor. |
40
|
|
|
* |
41
|
|
|
* @param string $tableName |
42
|
|
|
* @param string $viewName |
43
|
|
|
*/ |
44
|
3 |
|
public function __construct(string $tableName, string $viewName = "Grid view") |
45
|
|
|
{ |
46
|
3 |
|
$this->tableName = $tableName; |
47
|
3 |
|
$this->viewName = $viewName; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Client $client |
52
|
|
|
*/ |
53
|
3 |
|
public function setClient(Client $client): void |
54
|
|
|
{ |
55
|
3 |
|
$this->client = $client; |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string|null |
60
|
|
|
*/ |
61
|
2 |
|
public function getName(): ?string |
62
|
|
|
{ |
63
|
2 |
|
return $this->tableName; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* Params could include the following: |
69
|
|
|
* fields = ['column_1_name', 'column_2_name', 'column_3_name] |
70
|
|
|
* filterByFormula = "NOT({Headline} = '')" |
71
|
|
|
* maxRecords = 100 |
72
|
|
|
* pageSize = 100 |
73
|
|
|
* sort = [{field: "Headline", direction: "desc"}] |
74
|
|
|
* view = "view_name" |
75
|
|
|
* |
76
|
|
|
* @param array $params |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
*/ |
79
|
|
|
public function list(array $params): ResponseInterface |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$params = [ |
82
|
|
|
'maxRecords' => 3, |
83
|
|
|
'view' => $this->viewName |
84
|
|
|
]; |
85
|
|
|
|
86
|
|
|
// if (!empty($params)) |
87
|
|
|
$queryString = http_build_query($params); |
88
|
|
|
|
89
|
|
|
$url = $this->tableName . '?' . $queryString; |
90
|
|
|
|
91
|
|
|
return $this->client->request('GET', $url); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $records |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
1 |
|
public function create(array $records) |
99
|
|
|
{ |
100
|
1 |
|
return $this->client->send( |
101
|
1 |
|
TableRequest::createRecords($this->getName(), $records) |
|
|
|
|
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $id |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function read(string $id) |
110
|
|
|
{ |
111
|
|
|
return $this->client->request('GET', $this->tableName . '/' . $id); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @todo split out to a replace method for PUT |
116
|
|
|
* |
117
|
|
|
* @param array $data |
118
|
|
|
* @param string $type accepts PUT to replace or PATCH to update records |
119
|
|
|
* @return mixed |
120
|
|
|
* @throws \Exception |
121
|
|
|
*/ |
122
|
1 |
|
public function update(array $data, $type = 'PATCH') |
123
|
|
|
{ |
124
|
1 |
|
if (!in_array(strtolower($type), ['put', 'patch'])) { |
125
|
|
|
throw new \Exception('Invalid method type.'); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return $this->client->request( |
129
|
1 |
|
strtoupper($type), |
130
|
1 |
|
$this->tableName, |
131
|
|
|
[ |
132
|
|
|
'headers' => [ |
133
|
1 |
|
'Content-Type' => 'application/json', |
134
|
|
|
], |
135
|
1 |
|
'body' => json_encode($data), |
136
|
|
|
] |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $id |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function delete(string $id) |
145
|
|
|
{ |
146
|
|
|
return $this->client->request( |
147
|
|
|
'DELETE', |
148
|
|
|
$this->tableName, |
149
|
|
|
[ |
150
|
|
|
'query' => ['records[]' => $id], |
151
|
|
|
] |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.