Completed
Push — default-schema-host-name ( b35358 )
by Akihito
03:02
created

UriFactory::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Resource;
6
7
use BEAR\Resource\Annotation\ContextSchema;
8
use BEAR\Resource\Exception\UriException;
9
use function parse_url;
10
11
final class UriFactory
12
{
13
    /**
14
     * @var string
15
     */
16
    private $schemaHost;
17
18
    /**
19
     * @ContextSchema
20
     */
21
    public function __construct(string $schemaHost = 'page://self')
22
    {
23
        $this->schemaHost = $schemaHost;
24
    }
25
26
    public function __invoke($uri, array $query = [])
27
    {
28
        if (array_keys(parse_url($uri)) === ['path']) {
29
            $uri = $this->schemaHost . $uri;
30
        }
31
32
        return new Uri($uri, $query);
33
    }
34
35
    /**
36
     * @throws UriException
37
     */
38
    private function validate(string $uri)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
39
    {
40
        if (! filter_var($uri, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)) {
41
            $msg = is_string($uri) ? $uri : gettype($uri);
42
43
            throw new UriException($msg, 500);
44
        }
45
    }
46
}
47