Skip to content

Commit e9139ee

Browse files
committed
Allow stringable object to be used to geocode address
1 parent 60da2c7 commit e9139ee

File tree

4 files changed

+123
-2
lines changed

4 files changed

+123
-2
lines changed

src/Doctrine/ORM/GeocoderListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ private function geocodeEntity(ClassMetadata $metadata, object $entity): void
9292
$address = '';
9393
}
9494

95-
if (empty($address) || !is_string($address)) {
95+
if (empty($address) || (!is_string($address) && !$address instanceof \Stringable)) {
9696
return;
9797
}
9898

99-
$results = $this->geocoder->geocodeQuery(GeocodeQuery::create($address));
99+
$results = $this->geocoder->geocodeQuery(GeocodeQuery::create((string) $address));
100100

101101
if (!$results->isEmpty()) {
102102
$result = $results->first();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the BazingaGeocoderBundle 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 Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity;
14+
15+
use Bazinga\GeocoderBundle\Mapping\Attributes\Address;
16+
use Bazinga\GeocoderBundle\Mapping\Attributes\Geocodeable;
17+
use Bazinga\GeocoderBundle\Mapping\Attributes\Latitude;
18+
use Bazinga\GeocoderBundle\Mapping\Attributes\Longitude;
19+
use Doctrine\DBAL\Types\Types;
20+
use Doctrine\ORM\Mapping\Column;
21+
use Doctrine\ORM\Mapping\Embedded;
22+
use Doctrine\ORM\Mapping\Entity;
23+
use Doctrine\ORM\Mapping\GeneratedValue;
24+
use Doctrine\ORM\Mapping\Id;
25+
26+
#[Entity]
27+
#[Geocodeable]
28+
final class DummyWithStringableGetter
29+
{
30+
#[Id]
31+
#[GeneratedValue]
32+
#[Column(type: Types::INTEGER)]
33+
public $id;
34+
35+
#[Column]
36+
#[Latitude]
37+
public $latitude;
38+
39+
#[Column]
40+
#[Longitude]
41+
public $longitude;
42+
43+
#[Embedded]
44+
#[Address]
45+
public StringableAddress $address;
46+
47+
public function setAddress(StringableAddress $address): void
48+
{
49+
$this->address = $address;
50+
}
51+
52+
#[Address]
53+
public function getAddress(): StringableAddress
54+
{
55+
return $this->address;
56+
}
57+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the BazingaGeocoderBundle 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 Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity;
14+
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Embeddable;
17+
18+
#[Embeddable]
19+
final class StringableAddress implements \Stringable
20+
{
21+
public function __construct(
22+
#[Column]
23+
private string $address,
24+
) {
25+
}
26+
27+
public function __toString(): string
28+
{
29+
return $this->address;
30+
}
31+
}

tests/Functional/GeocoderListenerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity\DummyWithGetter;
1818
use Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity\DummyWithInvalidGetter;
1919
use Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity\DummyWithProperty;
20+
use Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity\DummyWithStringableGetter;
21+
use Bazinga\GeocoderBundle\Tests\Functional\Fixtures\Entity\StringableAddress;
2022
use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
2123
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
2224
use Doctrine\ORM\Configuration;
@@ -177,6 +179,37 @@ public function testPersistForGetter(): void
177179
self::assertNotEquals($clone->getLongitude(), $dummy->getLongitude());
178180
}
179181

182+
public function testPersistForStringableGetter(): void
183+
{
184+
self::bootKernel(['config' => static function (TestKernel $kernel) {
185+
$kernel->addTestConfig(__DIR__.'/config/framework.yml');
186+
187+
if ($kernel::VERSION_ID >= 60000) {
188+
$kernel->addTestConfig(__DIR__.'/config/framework_sf6.yml');
189+
}
190+
191+
$kernel->addTestConfig(__DIR__.'/config/listener.yml');
192+
$kernel->addTestConfig(__DIR__.'/config/listener_'.(PHP_VERSION_ID >= 80000 ? 'php8' : 'php7').'.yml');
193+
}]);
194+
195+
$container = self::getContainer();
196+
$container->set('http_client', self::createHttpClientForBerlinQuery());
197+
198+
$em = $container->get('doctrine.orm.entity_manager');
199+
200+
$tool = new SchemaTool($em);
201+
$tool->createSchema($em->getMetadataFactory()->getAllMetadata());
202+
203+
$dummy = new DummyWithStringableGetter();
204+
$dummy->setAddress(new StringableAddress('Berlin, Germany'));
205+
206+
$em->persist($dummy);
207+
$em->flush();
208+
209+
self::assertNotNull($dummy->latitude);
210+
self::assertNotNull($dummy->longitude);
211+
}
212+
180213
public function testPersistForInvalidGetter(): void
181214
{
182215
self::bootKernel(['config' => static function (TestKernel $kernel) {

0 commit comments

Comments
 (0)