Completed
Push — master ( 90b63b...634e39 )
by Tobias
06:06
created

ReverseQuery::fromCoordinates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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\Geocoder;
16
use Geocoder\Model\Coordinates;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class ReverseQuery
22
{
23
    /**
24
     * @var Coordinates
25
     */
26
    private $coordinates;
27
28
    /**
29
     * @var string|null
30
     */
31
    private $locale;
32
33
    /**
34
     * @var int
35
     */
36
    private $limit = Geocoder::DEFAULT_RESULT_LIMIT;
37
38
    /**
39
     * @var array
40
     */
41
    private $data = [];
42
43
    /**
44
     * @param Coordinates $coordinates
45
     */
46 4
    private function __construct(Coordinates $coordinates)
47
    {
48 4
        $this->coordinates = $coordinates;
49 4
    }
50
51
    /**
52
     * @param Coordinates $coordinates
53
     *
54
     * @return ReverseQuery
55
     */
56 1
    public static function create(Coordinates $coordinates)
57
    {
58 1
        return new self($coordinates);
59
    }
60
61
    /**
62
     * @param float $latitude
63
     * @param float $longitude
64
     *
65
     * @return ReverseQuery
66
     */
67 3
    public static function fromCoordinates($latitude, $longitude): ReverseQuery
68
    {
69 3
        return new self(new Coordinates($latitude, $longitude));
70
    }
71
72
    /**
73
     * @param Coordinates $coordinates
74
     *
75
     * @return ReverseQuery
76
     */
77
    public function withCoordinates(Coordinates $coordinates): ReverseQuery
78
    {
79
        $new = clone $this;
80
        $new->coordinates = $coordinates;
81
82
        return $new;
83
    }
84
85
    /**
86
     * @param int $limit
87
     *
88
     * @return ReverseQuery
89
     */
90 2
    public function withLimit(int $limit): ReverseQuery
91
    {
92 2
        $new = clone $this;
93 2
        $new->limit = $limit;
94
95 2
        return $new;
96
    }
97
98
    /**
99
     * @param string $locale
100
     *
101
     * @return ReverseQuery
102
     */
103 1
    public function withLocale(string $locale): ReverseQuery
104
    {
105 1
        $new = clone $this;
106 1
        $new->locale = $locale;
107
108 1
        return $new;
109
    }
110
111
    /**
112
     * @param string $name
113
     * @param mixed  $value
114
     *
115
     * @return ReverseQuery
116
     */
117 1
    public function withData(string $name, $value): ReverseQuery
118
    {
119 1
        $new = clone $this;
120 1
        $new->data[$name] = $value;
121
122 1
        return $new;
123
    }
124
125
    /**
126
     * @return Coordinates
127
     */
128 1
    public function getCoordinates(): Coordinates
129
    {
130 1
        return $this->coordinates;
131
    }
132
133
    /**
134
     * @return int
135
     */
136 2
    public function getLimit(): int
137
    {
138 2
        return $this->limit;
139
    }
140
141
    /**
142
     * @return string
143
     */
144 1
    public function getLocale()
145
    {
146 1
        return $this->locale;
147
    }
148
149
    /**
150
     * @param string     $name
151
     * @param mixed|null $default
152
     *
153
     * @return mixed
154
     */
155 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...
156
    {
157
        if (!array_key_exists($name, $this->data)) {
158
            return $default;
159
        }
160
161
        return $this->data[$name];
162
    }
163
164
    /**
165
     * @return array
166
     */
167 1
    public function getAllData(): array
168
    {
169 1
        return $this->data;
170
    }
171
172
    /**
173
     * String for logging. This is also a unique key for the query
174
     *
175
     * @return string
176
     */
177 1
    public function __toString()
178
    {
179 1
        return sprintf('ReverseQuery: %s', json_encode([
180 1
            'lat' => $this->getCoordinates()->getLatitude(),
181 1
            'lng' => $this->getCoordinates()->getLongitude(),
182 1
            'locale' => $this->getLocale(),
183 1
            'limit' => $this->getLimit(),
184 1
            'data' => $this->getAllData(),
185
        ]));
186
    }
187
}
188