Skip to content

Commit 56dd479

Browse files
authored
Merge pull request #5 from scottgrayson/where-method
testing and add methods: where, firstOrCreate, updateOrCreate
2 parents 347fadf + 61e3524 commit 56dd479

File tree

11 files changed

+316
-95
lines changed

11 files changed

+316
-95
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ build
22
composer.lock
33
docs
44
vendor
5-
coverage
5+
coverage
6+
.env
7+
.ac-php-conf.json

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,34 @@ AIRTABLE_TABLE=
3535

3636
## Usage
3737

38-
Get all records from that table.
39-
38+
#### Get all records from that table.
4039
``` php
4140
Airtable::table('tasks')->get();
4241
```
4342

44-
Get one record from the default table.
45-
43+
#### Get one record from the default table.
4644
``` php
4745
Airtable::find('id_string');
46+
47+
```
48+
49+
#### Filter records
50+
``` php
51+
Airtable::where('name', 'myName')->get();
52+
```
53+
54+
#### First or Create
55+
- First argument will be used for finding existing
56+
- Second argument is additional data to save if no results are found and we are creating (will not be saved used if item already exists)
57+
``` php
58+
Airtable::firstOrCreate(['name' => 'myName'], ['field' => 'myField']);
59+
```
60+
61+
#### Update or Create
62+
- First argument will be used to find existing
63+
- Second argument is additional data to save when we create or update
64+
``` php
65+
Airtable::createOrUpdate(['name' => 'myName'], ['field' => 'myField']);
4866
```
4967

5068
### Testing

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": "^7.1",
2020
"guzzlehttp/guzzle": "~6.0",
21-
"illuminate/support": "5.7.* || 5.8.*"
21+
"illuminate/support": "5.7.* || 5.8.*",
22+
"symfony/dotenv": "^4.2"
2223
},
2324
"require-dev": {
2425
"mockery/mockery": "^1.0",

config/config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@
4747

4848
],
4949

50+
'log_http' => env('AIRTABLE_LOG_HTTP', false),
51+
'log_http_format' => env('AIRTABLE_LOG_HTTP_FORMAT', '{request} >>> {res_body}'),
5052
];

env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TEST_AIRTABLE_API=true
2+
AIRTABLE_KEY=
3+
AIRTABLE_BASE=
4+
AIRTABLE_TABLE=

phpunit.xml.dist

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
2+
<phpunit bootstrap="tests/autoload.php"
33
backupGlobals="false"
44
backupStaticAttributes="false"
55
colors="true"
@@ -9,21 +9,24 @@
99
convertWarningsToExceptions="true"
1010
processIsolation="false"
1111
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
12+
<php>
13+
<env name="APP_ENV" value="test" />
14+
</php>
15+
<testsuites>
16+
<testsuite name="Test Suite">
17+
<directory>tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
<filter>
21+
<whitelist>
22+
<directory suffix=".php">src/</directory>
23+
</whitelist>
24+
</filter>
25+
<logging>
26+
<log type="tap" target="build/report.tap"/>
27+
<log type="junit" target="build/report.junit.xml"/>
28+
<log type="coverage-html" target="build/coverage"/>
29+
<log type="coverage-text" target="build/coverage.txt"/>
30+
<log type="coverage-clover" target="build/logs/clover.xml"/>
31+
</logging>
2932
</phpunit>

src/Airtable.php

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@ class Airtable
66
{
77
private $api;
88

9-
/** @var string */
10-
protected $base;
11-
12-
/** @var string */
13-
protected $table;
14-
15-
public function __construct($client, $table)
9+
public function __construct($client)
1610
{
17-
$this->table = $table;
1811
$this->api = $client;
1912
}
2013

@@ -40,11 +33,69 @@ public function destroy(string $id)
4033

4134
public function get()
4235
{
43-
return $this->api->get();
36+
return $this->toCollection($this->api->get());
4437
}
4538

4639
public function all()
4740
{
48-
return $this->api->getAllPages();
41+
return $this->toCollection($this->api->getAllPages());
42+
}
43+
44+
public function table($table)
45+
{
46+
$this->api->setTable($table);
47+
48+
return $this;
49+
}
50+
51+
public function where($column, $value)
52+
{
53+
return $this->api->addFilter($column, '=', $value);
54+
}
55+
56+
public function firstOrCreate(array $idData, array $createData = [])
57+
{
58+
foreach ($idData as $key => $value) {
59+
$this->where($key, $value);
60+
}
61+
62+
$results = $this->get();
63+
64+
// first
65+
if ($results->isNotEmpty()) {
66+
return $results->first();
67+
}
68+
69+
// create
70+
$data = array_merge($idData, $createData);
71+
72+
return $this->create($data);
73+
}
74+
75+
public function updateOrCreate(array $idData, array $updateData = [])
76+
{
77+
foreach ($idData as $key => $value) {
78+
$this->where($key, $value);
79+
}
80+
81+
$results = $this->get();
82+
83+
// first
84+
if ($results->isNotEmpty()) {
85+
$item = $results->first();
86+
87+
//update
88+
return $this->update($item['id'], $updateData);
89+
}
90+
91+
// create
92+
$data = array_merge($idData, $updateData);
93+
94+
return $this->create($data);
95+
}
96+
97+
private function toCollection($object)
98+
{
99+
return isset($object['records']) ? collect($object['records']) : $object;
49100
}
50101
}

src/AirtableManager.php

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ public function __construct($app)
3636
/**
3737
* Get a airtable table instance.
3838
*
39-
* @param string $name
39+
* @param string $table
4040
* @return \Airtable\Table
4141
*/
42-
public function table($name = null)
42+
public function table(string $table)
4343
{
44-
$name = $name ?: $this->getDefaultTable();
45-
46-
return $this->tables[$name] = $this->get($name);
44+
return $this->resolve($table);
4745
}
4846

4947
/**
@@ -91,41 +89,6 @@ public function setDefaultTable($name)
9189
$this->app['config']['airtable.default'] = $name;
9290
}
9391

94-
/**
95-
* Return all of the created connections.
96-
*
97-
* @return array
98-
*/
99-
public function getTables()
100-
{
101-
return $this->tables;
102-
}
103-
104-
/**
105-
* Set the given table instance.
106-
*
107-
* @param string $name
108-
* @param mixed $table
109-
* @return $this
110-
*/
111-
public function set($name, $table)
112-
{
113-
$this->tables[$name] = $table;
114-
115-
return $this;
116-
}
117-
118-
/**
119-
* Attempt to get the table from the local cache.
120-
*
121-
* @param string $name
122-
* @return \Tapp\Airtable
123-
*/
124-
protected function get($name)
125-
{
126-
return $this->tables[$name] ?? $this->resolve($name);
127-
}
128-
12992
/**
13093
* Resolve the given table.
13194
*
@@ -139,20 +102,26 @@ protected function resolve($name)
139102
$config = $this->getConfig($name);
140103

141104
if ($config) {
142-
return $this->createAirtable($config);
105+
return $this->createAirtable($config['name']);
143106
} else {
144107
throw new InvalidArgumentException("Table [{$name}] is not configured.");
145108
}
146109
}
147110

148-
protected function createAirtable($config)
111+
protected function createAirtable($table)
149112
{
150113
$base = $this->app['config']['airtable.base'];
151114
$access_token = $this->app['config']['airtable.key'];
152115

153-
$client = new AirtableApiClient($base, $config, $access_token);
116+
if ($this->app['config']['airtable.log_http']) {
117+
$httpLogFormat = $this->app['config']['airtable.log_http_format'];
118+
} else {
119+
$httpLogFormat = null;
120+
}
121+
122+
$client = new AirtableApiClient($base, $table, $access_token, $httpLogFormat);
154123

155-
return new Airtable($client, $config);
124+
return new Airtable($client, $table);
156125
}
157126

158127
/**

0 commit comments

Comments
 (0)