Service::query()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php declare(strict_types=1);
2
3
namespace JSKOS;
4
5
const QueryModifiers = [
6
    "limit", "offset", "properties", "callback",
7
];
8
9
/**
10
 * JSKOS API backend.
11
 */
12
abstract class Service
13
{
14
    /**
15
     * List of supported query parameters.
16
     * @var array
17
     */
18
    protected $supportedParameters = [];
19
20
    /**
21
     * Perform a query.
22
     *
23
     * @return Result
24
     * @throws Error
25
     */
26
    abstract public function query(array $request, string $path=''): Result;
27
28
    /**
29
     * Enable support of a query parameter.
30
     * @param string $name
31
     */
32
    public function supportParameter($name)
33
    {
34
        if (in_array($name, QueryModifiers)) {
35
            throw new \DomainException("parameter $name not allowed");
36
        }
37
        $this->supportedParameters[$name] = $name;
38
        asort($this->supportedParameters);
39
    }
40
41
    /**
42
     * Get a list of supported query parameters.
43
     * @return array
44
     */
45
    public function getSupportedParameters()
46
    {
47
        return $this->supportedParameters;
48
    }
49
50
    /**
51
     * Get a list of query parameters as URI template.
52
     *
53
     * @return string
54
     */
55
    public function uriTemplate($template='')
56
    {
57
        foreach ($this->supportedParameters as $name) {
58
            $template .= "{?$name}";
59
        }
60
        return $template;
61
    }
62
}
63