-
-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathValidatedActionCodeSettings.php
More file actions
132 lines (99 loc) · 3.49 KB
/
ValidatedActionCodeSettings.php
File metadata and controls
132 lines (99 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Auth\ActionCodeSettings;
use GuzzleHttp\Psr7\Utils;
use Kreait\Firebase\Auth\ActionCodeSettings;
use Kreait\Firebase\Exception\InvalidArgumentException;
use Psr\Http\Message\UriInterface;
use Stringable;
use function array_filter;
use function is_bool;
use function is_string;
use function mb_strtolower;
final class ValidatedActionCodeSettings implements ActionCodeSettings
{
private ?UriInterface $continueUrl = null;
private ?bool $canHandleCodeInApp = null;
/**
* @var non-empty-string|null
*/
private ?string $androidPackageName = null;
/**
* @var non-empty-string|null
*/
private ?string $androidMinimumVersion = null;
private ?bool $androidInstallApp = null;
/**
* @var non-empty-string|null
*/
private ?string $iOSBundleId = null;
private function __construct()
{
}
public static function empty(): self
{
return new self();
}
/**
* @param array<non-empty-string, mixed> $settings
*/
public static function fromArray(array $settings): self
{
$instance = new self();
$settings = array_filter($settings, static fn($value): bool => $value !== null);
foreach ($settings as $key => $value) {
switch (mb_strtolower($key)) {
case 'continueurl':
case 'url':
$instance->continueUrl = Utils::uriFor(self::ensureNonEmptyString($value));
break;
case 'handlecodeinapp':
$instance->canHandleCodeInApp = (bool) $value;
break;
case 'androidpackagename':
$instance->androidPackageName = self::ensureNonEmptyString($value);
break;
case 'androidminimumversion':
$instance->androidMinimumVersion = self::ensureNonEmptyString($value);
break;
case 'androidinstallapp':
$instance->androidInstallApp = (bool) $value;
break;
case 'iosbundleid':
$instance->iOSBundleId = self::ensureNonEmptyString($value);
break;
default:
throw new InvalidArgumentException("Unsupported action code setting '{$key}'");
}
}
return $instance;
}
/**
* @return array<non-empty-string, bool|non-empty-string>
*/
public function toArray(): array
{
$continueUrl = $this->continueUrl !== null ? (string) $this->continueUrl : null;
return array_filter([
'continueUrl' => $continueUrl,
'canHandleCodeInApp' => $this->canHandleCodeInApp,
'androidPackageName' => $this->androidPackageName,
'androidMinimumVersion' => $this->androidMinimumVersion,
'androidInstallApp' => $this->androidInstallApp,
'iOSBundleId' => $this->iOSBundleId,
], static fn(string|bool|null $value): bool => is_bool($value) || (is_string($value) && $value !== ''));
}
/**
* @return non-empty-string
*/
private static function ensureNonEmptyString(mixed $value): string
{
if ($value instanceof Stringable) {
$value = (string) $value;
}
if (!is_string($value) || $value === '') {
throw new InvalidArgumentException('A non-empty string is required');
}
return $value;
}
}