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

ReverseQuery::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\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 3
    private function __construct(Coordinates $coordinates)
47
    {
48 3
        $this->coordinates = $coordinates;
49 3
    }
50
51
    /**
52
     * @param Coordinates $coordinates
53
     *
54
     * @return ReverseQuery
55
     */
56
    public static function create(Coordinates $coordinates)
57
    {
58
        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 int $limit
74
     *
75
     * @return ReverseQuery
76
     */
77 1
    public function withLimit(int $limit): ReverseQuery
78
    {
79 1
        $new = clone $this;
80 1
        $new->limit = $limit;
81
82 1
        return $new;
83
    }
84
85
    /**
86
     * @param string $locale
87
     *
88
     * @return ReverseQuery
89
     */
90 1
    public function withLocale(string $locale): ReverseQuery
91
    {
92 1
        $new = clone $this;
93 1
        $new->locale = $locale;
94
95 1
        return $new;
96
    }
97
98
    /**
99
     * @param string $name
100
     * @param mixed  $value
101
     *
102
     * @return ReverseQuery
103
     */
104 1
    public function withData(string $name, $value): ReverseQuery
105
    {
106 1
        $new = clone $this;
107 1
        $new->data[$name] = $value;
108
109 1
        return $new;
110
    }
111
112
    /**
113
     * @return Coordinates
114
     */
115 1
    public function getCoordinates(): Coordinates
116
    {
117 1
        return $this->coordinates;
118
    }
119
120
    /**
121
     * @return int
122
     */
123 1
    public function getLimit(): int
124
    {
125 1
        return $this->limit;
126
    }
127
128
    /**
129
     * @return string
130
     */
131 1
    public function getLocale()
132
    {
133 1
        return $this->locale;
134
    }
135
136
    /**
137
     * @param string     $name
138
     * @param mixed|null $default
139
     *
140
     * @return mixed
141
     */
142 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...
143
    {
144
        if (!array_key_exists($name, $this->data)) {
145
            return $default;
146
        }
147
148
        return $this->data[$name];
149
    }
150
151
    /**
152
     * @return array
153
     */
154 1
    public function getAllData(): array
155
    {
156 1
        return $this->data;
157
    }
158
159
    /**
160
     * String for logging. This is also a unique key for the query
161
     *
162
     * @return string
163
     */
164 1
    public function __toString()
165
    {
166 1
        return sprintf('ReverseQuery: %s', json_encode([
167 1
            'lat' => $this->getCoordinates()->getLatitude(),
168 1
            'lng' => $this->getCoordinates()->getLongitude(),
169 1
            'locale' => $this->getLocale(),
170 1
            'limit' => $this->getLimit(),
171 1
            'data' => $this->getAllData(),
172
        ]));
173
    }
174
}
175