Completed
Push — master ( 2f5933...8ec5a6 )
by Oscar
02:10
created

JsonSchema::errorHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class JsonSchema
9
{
10
    /** @var string[] */
11
    private $schemas;
12
13
    /** @var callable|null */
14
    private $errorHandler;
15
16
    /**
17
     * JsonSchema constructor.
18
     *
19
     * @param string[] $schemas [uri => file] An associative array of HTTP URI to validation schema
20
     */
21
    public function __construct(array $schemas)
22
    {
23
        $this->schemas = $schemas;
24
    }
25
26
    /**
27
     * Execute the middleware.
28
     *
29
     * @param ServerRequestInterface $request
30
     * @param ResponseInterface      $response
31
     * @param callable               $next
32
     *
33
     * @return ResponseInterface
34
     */
35
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
36
    {
37
        $schema = $this->getSchema($request);
38
39
        if ($schema instanceof \SplFileObject) {
40
            $validator = JsonValidator::fromFile($schema);
41
            if (is_callable($this->errorHandler)) {
42
                $validator->errorHandler($this->errorHandler);
43
            }
44
45
            return $validator($request, $response, $next);
46
        }
47
48
        return $next($request, $response);
49
    }
50
51
    /**
52
     * @param ServerRequestInterface $request
53
     *
54
     * @return \SplFileObject|null
55
     */
56
    private function getSchema(ServerRequestInterface $request)
57
    {
58
        $uri = $request->getUri();
59
        $path = $uri->getPath();
60
61
        foreach ($this->schemas as $pattern => $file) {
62
            if (stripos($path, $pattern) === 0) {
63
                return new \SplFileObject($this->normalizeFilePath($file));
64
            }
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * @param string
72
     *
73
     * @return string
74
     */
75
    private function normalizeFilePath($path)
76
    {
77
        if (parse_url($path, PHP_URL_SCHEME)) {
78
            // The schema file already has a scheme, e.g. `file://` or `vfs://`.
79
            return $path;
80
        }
81
82
        return 'file://'.$path;
83
    }
84
85
    /**
86
     * Has the following method signature:
87
     * function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {}
88
     *
89
     * Validation errors are stored in a middleware attribute:
90
     * $request->getAttribute(Middleware::KEY, [])[JsonValidator::KEY];
91
     *
92
     * @param callable $errorHandler
93
     * @return void
94
     */
95
    public function errorHandler(callable $errorHandler)
96
    {
97
        $this->errorHandler = $errorHandler;
98
    }
99
}
100