Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b132d44

Browse files
authored
Added __toString on queries (#718)
* Added __toString on queries * Applied changes from StyleCI
1 parent 50d72d7 commit b132d44

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/Common/Query/GeocodeQuery.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,20 @@ public function getAllData(): array
177177
{
178178
return $this->data;
179179
}
180+
181+
/**
182+
* String for logging. This is also a unique key for the query
183+
*
184+
* @return string
185+
*/
186+
public function __toString()
187+
{
188+
return sprintf('GeocodeQuery: %s', json_encode([
189+
'text' => $this->getText(),
190+
'bounds' => $this->getBounds() ? $this->getBounds()->toArray() : 'null',
191+
'locale' => $this->getLocale(),
192+
'limit' => $this->getLimit(),
193+
'data' => $this->getAllData(),
194+
]));
195+
}
180196
}

src/Common/Query/ReverseQuery.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,20 @@ public function getAllData(): array
155155
{
156156
return $this->data;
157157
}
158+
159+
/**
160+
* String for logging. This is also a unique key for the query
161+
*
162+
* @return string
163+
*/
164+
public function __toString()
165+
{
166+
return sprintf('ReverseQuery: %s', json_encode([
167+
'lat' => $this->getCoordinates()->getLatitude(),
168+
'lng' => $this->getCoordinates()->getLongitude(),
169+
'locale' => $this->getLocale(),
170+
'limit' => $this->getLimit(),
171+
'data' => $this->getAllData(),
172+
]));
173+
}
158174
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Tests;
12+
13+
use Geocoder\Query\GeocodeQuery;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class GeocodeQueryTest extends TestCase
20+
{
21+
public function testToString()
22+
{
23+
$query = GeocodeQuery::create('foo');
24+
$query = $query->withLocale('en');
25+
$query = $query->withLimit(3);
26+
$query = $query->withData('name', 'value');
27+
28+
$string = $query->__toString();
29+
$this->assertContains('GeocodeQuery', $string);
30+
$this->assertContains('"text":"foo"', $string);
31+
$this->assertContains('"locale":"en"', $string);
32+
$this->assertContains('"limit":3', $string);
33+
$this->assertContains('"name":"value"', $string);
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Tests;
12+
13+
use Geocoder\Query\ReverseQuery;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class ReverseQueryTest extends TestCase
20+
{
21+
public function testToString()
22+
{
23+
$query = ReverseQuery::fromCoordinates(1, 2);
24+
$query = $query->withLocale('en');
25+
$query = $query->withLimit(3);
26+
$query = $query->withData('name', 'value');
27+
28+
$string = $query->__toString();
29+
$this->assertContains('ReverseQuery', $string);
30+
$this->assertContains('"lat":1', $string);
31+
$this->assertContains('"lng":2', $string);
32+
$this->assertContains('"locale":"en"', $string);
33+
$this->assertContains('"limit":3', $string);
34+
$this->assertContains('"name":"value"', $string);
35+
}
36+
}

0 commit comments

Comments
 (0)