1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Beachcasts\Airtable; |
6
|
|
|
|
7
|
|
|
use Assert\Assert; |
8
|
|
|
|
9
|
|
|
class Config |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $baseUrl; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $version; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $apiKey; |
25
|
|
|
|
26
|
8 |
|
protected function __construct(string $baseUrl, string $version, string $apiKey) |
27
|
|
|
{ |
28
|
8 |
|
$this->baseUrl = $baseUrl; |
29
|
8 |
|
$this->version = $version; |
30
|
8 |
|
$this->apiKey = $apiKey; |
31
|
8 |
|
} |
32
|
|
|
|
33
|
5 |
|
public static function fromEnvironment(): Config |
34
|
|
|
{ |
35
|
5 |
|
$baseUrl = getenv('BASE_URL'); |
36
|
5 |
|
$version = getenv('VERSION'); |
37
|
5 |
|
$apiKey = getenv('API_KEY'); |
38
|
|
|
|
39
|
5 |
|
self::validateValues($baseUrl, $version, $apiKey); |
40
|
|
|
|
41
|
4 |
|
return new self($baseUrl, $version, $apiKey); |
42
|
|
|
} |
43
|
|
|
|
44
|
8 |
|
public static function fromValues(string $baseUrl, string $version, string $apiKey): Config |
45
|
|
|
{ |
46
|
8 |
|
self::validateValues($baseUrl, $version, $apiKey); |
47
|
|
|
|
48
|
4 |
|
return new self($baseUrl, $version, $apiKey); |
49
|
|
|
} |
50
|
|
|
|
51
|
12 |
|
protected static function validateValues(string $baseUrl, string $version, string $apiKey): bool |
52
|
|
|
{ |
53
|
12 |
|
Assert::that($baseUrl) |
54
|
12 |
|
->notBlank('baseUrl was blank/empty') |
55
|
11 |
|
->url('baseUrl was not a valid url'); |
56
|
|
|
|
57
|
10 |
|
Assert::that($version) |
58
|
10 |
|
->regex('/v\d+/', 'Version did not match expected pattern'); |
59
|
|
|
|
60
|
9 |
|
Assert::that($apiKey) |
61
|
9 |
|
->notBlank('apiKey was blank/empty'); |
62
|
|
|
|
63
|
8 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
public function getBaseUrl(): string |
67
|
|
|
{ |
68
|
7 |
|
return $this->baseUrl; |
69
|
|
|
} |
70
|
|
|
|
71
|
7 |
|
public function getVersion(): string |
72
|
|
|
{ |
73
|
7 |
|
return $this->version; |
74
|
|
|
} |
75
|
|
|
|
76
|
7 |
|
public function getApiKey(): string |
77
|
|
|
{ |
78
|
7 |
|
return $this->apiKey; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|