Completed
Push — master ( 175b7e...79e53c )
by Tobias
01:57
created

GeocodeQuery::withText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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 implements Query
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 6
    private function __construct(string $text)
55
    {
56 6
        if (empty($text)) {
57
            throw new InvalidArgument('Geocode query cannot be empty');
58
        }
59
60 6
        $this->text = $text;
61 6
    }
62
63
    /**
64
     * @param string $text
65
     *
66
     * @return GeocodeQuery
67
     */
68 6
    public static function create(string $text): GeocodeQuery
69
    {
70 6
        return new self($text);
71
    }
72
73
    /**
74
     * @param string $text
75
     *
76
     * @return GeocodeQuery
77
     */
78
    public function withText(string $text): GeocodeQuery
79
    {
80
        $new = clone $this;
81
        $new->text = $text;
82
83
        return $new;
84
    }
85
86
    /**
87
     * @param Bounds $bounds
88
     *
89
     * @return GeocodeQuery
90
     */
91
    public function withBounds(Bounds $bounds): GeocodeQuery
92
    {
93
        $new = clone $this;
94
        $new->bounds = $bounds;
95
96
        return $new;
97
    }
98
99
    /**
100
     * @param string $locale
101
     *
102
     * @return GeocodeQuery
103
     */
104 1
    public function withLocale(string $locale): GeocodeQuery
105
    {
106 1
        $new = clone $this;
107 1
        $new->locale = $locale;
108
109 1
        return $new;
110
    }
111
112
    /**
113
     * @param int $limit
114
     *
115
     * @return GeocodeQuery
116
     */
117 2
    public function withLimit(int $limit): GeocodeQuery
118
    {
119 2
        $new = clone $this;
120 2
        $new->limit = $limit;
121
122 2
        return $new;
123
    }
124
125
    /**
126
     * @param string $name
127
     * @param mixed  $value
128
     *
129
     * @return GeocodeQuery
130
     */
131 1
    public function withData(string $name, $value): GeocodeQuery
132
    {
133 1
        $new = clone $this;
134 1
        $new->data[$name] = $value;
135
136 1
        return $new;
137
    }
138
139
    /**
140
     * @return string
141
     */
142 1
    public function getText(): string
143
    {
144 1
        return $this->text;
145
    }
146
147
    /**
148
     * @return Bounds|null
149
     */
150 1
    public function getBounds()
151
    {
152 1
        return $this->bounds;
153
    }
154
155
    /**
156
     * @return string|null
157
     */
158 1
    public function getLocale()
159
    {
160 1
        return $this->locale;
161
    }
162
163
    /**
164
     * @return int
165
     */
166 2
    public function getLimit(): int
167
    {
168 2
        return $this->limit;
169
    }
170
171
    /**
172
     * @param string     $name
173
     * @param mixed|null $default
174
     *
175
     * @return mixed
176
     */
177 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...
178
    {
179
        if (!array_key_exists($name, $this->data)) {
180
            return $default;
181
        }
182
183
        return $this->data[$name];
184
    }
185
186
    /**
187
     * @return array
188
     */
189 1
    public function getAllData(): array
190
    {
191 1
        return $this->data;
192
    }
193
194
    /**
195
     * String for logging. This is also a unique key for the query
196
     *
197
     * @return string
198
     */
199 1
    public function __toString()
200
    {
201 1
        return sprintf('GeocodeQuery: %s', json_encode([
202 1
            'text' => $this->getText(),
203 1
            'bounds' => $this->getBounds() ? $this->getBounds()->toArray() : 'null',
204 1
            'locale' => $this->getLocale(),
205 1
            'limit' => $this->getLimit(),
206 1
            'data' => $this->getAllData(),
207
        ]));
208
    }
209
}
210