1 <?php
2
3 namespace Http\Message\Authentication;
4
5 use Http\Message\Authentication;
6 use Psr\Http\Message\RequestInterface;
7
8 9 10 11 12
13 final class Matching implements Authentication
14 {
15 16 17
18 private $authentication;
19
20 21 22
23 private $matcher;
24
25 26 27 28
29 public function __construct(Authentication $authentication, callable $matcher = null)
30 {
31 if (is_null($matcher)) {
32 $matcher = function () {
33 return true;
34 };
35 }
36
37 $this->authentication = $authentication;
38 $this->matcher = $matcher;
39 }
40
41 42 43
44 public function authenticate(RequestInterface $request)
45 {
46 if (!call_user_func($this->matcher, $request)) {
47 return $request;
48 }
49
50 return $this->authentication->authenticate($request);
51 }
52
53 54 55 56 57 58 59 60
61 public static function createUrlMatcher(Authentication $authentication, $url)
62 {
63 $matcher = function ($request) use ($url) {
64 return preg_match($url, $request->getRequestTarget());
65 };
66
67 return new static($authentication, $matcher);
68 }
69 }
70