Completed
Push — master ( 4dcdcf...d95589 )
by Mr
03:36
created

Query::setAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\QueryException;
6
use RouterOS\Interfaces\QueryInterface;
7
8
/**
9
 * Class Query for building queries
10
 *
11
 * @package RouterOS
12
 * @since   0.1
13
 */
14
class Query implements QueryInterface
15
{
16
    /**
17
     * Array of query attributes
18
     *
19
     * @var array
20
     */
21
    private $_attributes = [];
22
23
    /**
24
     * Endpoint of query
25
     *
26
     * @var string
27
     */
28
    private $_endpoint;
29
30
    /**
31
     * Query constructor.
32
     *
33
     * @param   string $endpoint   Path of endpoint
34
     * @param   array  $attributes List of attributes which should be set
35
     */
36 13
    public function __construct(string $endpoint = null, array $attributes = [])
37
    {
38
        // TODO: Endpoint may be array, first line will be endpoint, any other attributes
39 13
        $this->setEndpoint($endpoint);
40 13
        $this->setAttributes($attributes);
41 13
    }
42
43
    /**
44
     * Append to array yet another attribute of query
45
     *
46
     * @param   string $word
47
     * @return  \RouterOS\Query
48
     */
49 6
    public function add(string $word): Query
50
    {
51 6
        $this->_attributes[] = $word;
52 6
        return $this;
53
    }
54
55
    /**
56
     * Get attributes array of current query
57
     *
58
     * @return  array
59
     */
60 8
    public function getAttributes(): array
61
    {
62 8
        return $this->_attributes;
63
    }
64
65
    /**
66
     * Set array of attributes
67
     *
68
     * @param   array $attributes
69
     * @since   0.7
70
     * @return  \RouterOS\Query
71
     */
72 13
    public function setAttributes(array $attributes): Query
73
    {
74 13
        $this->_attributes = $attributes;
75 13
        return $this;
76
    }
77
78
    /**
79
     * Get endpoint of current query
80
     *
81
     * @return  string|null
82
     */
83 8
    public function getEndpoint()
84
    {
85 8
        return $this->_endpoint;
86
    }
87
88
    /**
89
     * Set endpoint of query
90
     *
91
     * @param   string|null $endpoint
92
     * @since   0.7
93
     * @return  \RouterOS\Query
94
     */
95 13
    public function setEndpoint(string $endpoint = null): Query
96
    {
97 13
        $this->_endpoint = $endpoint;
98 13
        return $this;
99
    }
100
101
    /**
102
     * Build body of query
103
     *
104
     * @return  array
105
     * @throws  \RouterOS\Exceptions\QueryException
106
     */
107 6
    public function getQuery(): array
108
    {
109 6
        if ($this->getEndpoint() === null) {
110 1
            throw new QueryException('Endpoint of query is not set');
111
        }
112
113 5
        $endpoint   = $this->getEndpoint();
114 5
        $attributes = $this->getAttributes();
115 5
        array_unshift($attributes, $endpoint);
116
117 5
        return $attributes;
118
    }
119
}
120