-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViaForm.php
More file actions
50 lines (45 loc) · 1.01 KB
/
ViaForm.php
File metadata and controls
50 lines (45 loc) · 1.01 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
<?php
declare(strict_types = 1);
namespace Innmind\HttpAuthentication;
use Innmind\Http\{
ServerRequest,
ServerRequest\Form,
Method,
};
use Innmind\Immutable\{
Maybe,
Attempt,
};
/**
* @template T
*/
final class ViaForm
{
/**
* @param \Closure(Form): Attempt<T> $resolve
*/
private function __construct(private \Closure $resolve)
{
}
/**
* @return Attempt<T>
*/
public function __invoke(ServerRequest $request): Attempt
{
return Maybe::just($request)
->filter(static fn($request) => $request->method() === Method::post)
->attempt(static fn() => new \RuntimeException('Failed to resolve identity'))
->flatMap(fn($request) => ($this->resolve)($request->form()));
}
/**
* @template A
*
* @param callable(Form): Attempt<A> $resolve
*
* @return self<A>
*/
public static function of(callable $resolve): self
{
return new self(\Closure::fromCallable($resolve));
}
}