1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tapp\Airtable; |
4
|
|
|
|
5
|
|
|
class Airtable |
6
|
|
|
{ |
7
|
|
|
private $api; |
8
|
|
|
|
9
|
|
|
/** @var string */ |
10
|
|
|
protected $base; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $table; |
14
|
|
|
|
15
|
|
|
public function __construct($client, $table) |
16
|
|
|
{ |
17
|
|
|
$this->table = $table; |
18
|
|
|
$this->api = $client; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function find(string $id) |
22
|
|
|
{ |
23
|
|
|
return $this->api->get($id); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function create($data) |
27
|
|
|
{ |
28
|
|
|
return $this->api->post($data); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function update(string $id, $data) |
32
|
|
|
{ |
33
|
|
|
return $this->api->put($id, $data); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function destroy(string $id) |
37
|
|
|
{ |
38
|
|
|
return $this->api->delete($id); |
39
|
|
|
} |
40
|
|
|
public function get() |
41
|
|
|
{ |
42
|
|
|
return $this->toCollection($this->api->get()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function all() |
46
|
|
|
{ |
47
|
|
|
return $this->toCollection($this->api->getAllPages()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function table($table) |
51
|
|
|
{ |
52
|
|
|
$this->api->table($table); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function where($column, $value) |
58
|
|
|
{ |
59
|
|
|
return $this->api->addFilter($column, '=', $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function firstOrCreate(array $idData, array $createData = []) |
63
|
|
|
{ |
64
|
|
|
foreach ($idData as $key => $value) { |
65
|
|
|
$this->where($key, $value); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$results = $this->get(); |
69
|
|
|
|
70
|
|
|
// first |
71
|
|
|
if ($results->isNotEmpty()) { |
72
|
|
|
return $results->first(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// create |
76
|
|
|
$data = array_merge($idData, $createData); |
77
|
|
|
|
78
|
|
|
return $this->create($data); |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function createOrUpdate(array $idData, array $updateData = []) |
83
|
|
|
{ |
84
|
|
|
foreach ($idData as $key => $value) { |
85
|
|
|
$this->where($key, $value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$results = $this->get(); |
89
|
|
|
|
90
|
|
|
// first |
91
|
|
|
if ($results->isNotEmpty()) { |
92
|
|
|
$item = $results->first(); |
93
|
|
|
|
94
|
|
|
//update |
95
|
|
|
return $this->update($item->id, $updateData); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// create |
99
|
|
|
$data = array_merge($idData, $updateData); |
100
|
|
|
|
101
|
|
|
return $this->create($data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function toCollection($object) |
105
|
|
|
{ |
106
|
|
|
return isset($object['records']) ? collect($object['records']) : $object; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|