Completed
Push — master ( da35d1...9894be )
by Tobias
02:24
created

GeocodeQuery::__toString()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Query;
14
15
use Geocoder\Exception\InvalidArgument;
16
use Geocoder\Geocoder;
17
use Geocoder\Model\Bounds;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
final class GeocodeQuery
23
{
24
    /**
25
     * The address or text that should be geocoded.
26
     *
27
     * @var string
28
     */
29
    private $text;
30
31
    /**
32
     * @var Bounds|null
33
     */
34
    private $bounds;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $locale;
40
41
    /**
42
     * @var int
43
     */
44
    private $limit = Geocoder::DEFAULT_RESULT_LIMIT;
45
46
    /**
47
     * @var array
48
     */
49
    private $data = [];
50
51
    /**
52
     * @param string $text
53
     */
54 3
    private function __construct(string $text)
55
    {
56 3
        if (empty($text)) {
57
            throw new InvalidArgument('Geocode query cannot be empty');
58
        }
59
60 3
        $this->text = $text;
61 3
    }
62
63
    /**
64
     * @param string $text
65
     *
66
     * @return GeocodeQuery
67
     */
68 3
    public static function create(string $text): GeocodeQuery
69
    {
70 3
        return new self($text);
71
    }
72
73
    /**
74
     * @param Bounds $bounds
75
     *
76
     * @return GeocodeQuery
77
     */
78
    public function withBounds(Bounds $bounds): GeocodeQuery
79
    {
80
        $new = clone $this;
81
        $new->bounds = $bounds;
82
83
        return $new;
84
    }
85
86
    /**
87
     * @param string $locale
88
     *
89
     * @return GeocodeQuery
90
     */
91 1
    public function withLocale(string $locale): GeocodeQuery
92
    {
93 1
        $new = clone $this;
94 1
        $new->locale = $locale;
95
96 1
        return $new;
97
    }
98
99
    /**
100
     * @param int $limit
101
     *
102
     * @return GeocodeQuery
103
     */
104 1
    public function withLimit(int $limit): GeocodeQuery
105
    {
106 1
        $new = clone $this;
107 1
        $new->limit = $limit;
108
109 1
        return $new;
110
    }
111
112
    /**
113
     * @param string $name
114
     * @param mixed  $value
115
     *
116
     * @return GeocodeQuery
117
     */
118 1
    public function withData(string $name, $value): GeocodeQuery
119
    {
120 1
        $new = clone $this;
121 1
        $new->data[$name] = $value;
122
123 1
        return $new;
124
    }
125
126
    /**
127
     * @return string
128
     */
129 1
    public function getText(): string
130
    {
131 1
        return $this->text;
132
    }
133
134
    /**
135
     * @return Bounds|null
136
     */
137 1
    public function getBounds()
138
    {
139 1
        return $this->bounds;
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145 1
    public function getLocale()
146
    {
147 1
        return $this->locale;
148
    }
149
150
    /**
151
     * @return int
152
     */
153 1
    public function getLimit(): int
154
    {
155 1
        return $this->limit;
156
    }
157
158
    /**
159
     * @param string     $name
160
     * @param mixed|null $default
161
     *
162
     * @return mixed
163
     */
164 View Code Duplication
    public function getData(string $name, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
    {
166
        if (!array_key_exists($name, $this->data)) {
167
            return $default;
168
        }
169
170
        return $this->data[$name];
171
    }
172
173
    /**
174
     * @return array
175
     */
176 1
    public function getAllData(): array
177
    {
178 1
        return $this->data;
179
    }
180
181
    /**
182
     * String for logging. This is also a unique key for the query
183
     *
184
     * @return string
185
     */
186 1
    public function __toString()
187
    {
188 1
        return sprintf('GeocodeQuery: %s', json_encode([
189 1
            'text' => $this->getText(),
190 1
            'bounds' => $this->getBounds() ? $this->getBounds()->toArray() : 'null',
191 1
            'locale' => $this->getLocale(),
192 1
            'limit' => $this->getLimit(),
193 1
            'data' => $this->getAllData(),
194
        ]));
195
    }
196
}
197