Skip to content

Commit 80d51ce

Browse files
authored
Merge pull request #382 from norkunas/stringable-getter
Allow stringable object to be used to geocode address
2 parents 60da2c7 + f3f2931 commit 80d51ce

File tree

5 files changed

+139
-5
lines changed

5 files changed

+139
-5
lines changed

src/Doctrine/ORM/GeocoderListener.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function onFlush(OnFlushEventArgs $args): void
5757
$this->geocodeEntity($metadata, $entity);
5858

5959
$uow->recomputeSingleEntityChangeSet(
60-
$em->getClassMetadata(get_class($entity)),
60+
$em->getClassMetadata($entity::class),
6161
$entity
6262
);
6363
}
@@ -76,7 +76,7 @@ public function onFlush(OnFlushEventArgs $args): void
7676
$this->geocodeEntity($metadata, $entity);
7777

7878
$uow->recomputeSingleEntityChangeSet(
79-
$em->getClassMetadata(get_class($entity)),
79+
$em->getClassMetadata($entity::class),
8080
$entity
8181
);
8282
}
@@ -92,11 +92,16 @@ private function geocodeEntity(ClassMetadata $metadata, object $entity): void
9292
$address = '';
9393
}
9494

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

99-
$results = $this->geocoder->geocodeQuery(GeocodeQuery::create($address));
99+
$addressString = (string) $address;
100+
if ('' === $addressString) {
101+
return;
102+
}
103+
104+
$results = $this->geocoder->geocodeQuery(GeocodeQuery::create($addressString));
100105

101106
if (!$results->isEmpty()) {
102107
$result = $results->first();

src/Mapping/Driver/AttributeDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function loadMetadataFromObject(object $object): ClassMetadata
4040
$attributes = $reflection->getAttributes(Attributes\Geocodeable::class);
4141

4242
if ([] === $attributes) {
43-
throw new MappingException(sprintf('The class "%s" is not geocodeable', get_class($object)));
43+
throw new MappingException(sprintf('The class "%s" is not geocodeable', $object::class));
4444
}
4545

4646
$args = [];
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
public StringableAddress $address;
45+
46+
public function setAddress(StringableAddress $address): void
47+
{
48+
$this->address = $address;
49+
}
50+
51+
#[Address]
52+
public function getAddress(): StringableAddress
53+
{
54+
return $this->address;
55+
}
56+
}
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: 42 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,46 @@ 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+
$clone = clone $dummy;
213+
$dummy->setAddress(new StringableAddress('Paris, France'));
214+
215+
$em->persist($dummy);
216+
$em->flush();
217+
218+
self::assertNotEquals($clone->latitude, $dummy->latitude);
219+
self::assertNotEquals($clone->longitude, $dummy->longitude);
220+
}
221+
180222
public function testPersistForInvalidGetter(): void
181223
{
182224
self::bootKernel(['config' => static function (TestKernel $kernel) {

0 commit comments

Comments
 (0)