Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,15 @@ jobs:
max-parallel: 10
fail-fast: false
matrix:
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
sf_version: [ '5.4.*', '6.4.*', '7.2.*', '7.3.*' ]
php: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
sf_version: [ '6.4.*', '7.2.*', '7.3.*' ]
exclude:
- php: '7.4'
sf_version: '6.4.*'
- php: '8.0'
sf_version: '6.4.*'
- php: '7.4'
sf_version: '7.2.*'
- php: '8.0'
sf_version: '7.2.*'
- php: '8.1'
sf_version: '7.2.*'
- php: '7.4'
sf_version: '7.3.*'
- php: '8.0'
sf_version: '7.3.*'
- php: '8.1'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
composer.lock
phpunit.xml
/config/reference.php
vendor/
.php-cs-fixer.cache
.phpunit.result.cache
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
->setRules([
'@Symfony' => true,
'no_superfluous_phpdoc_tags' => false,
'phpdoc_to_comment' => ['ignored_tags' => ['var']], // phpstan errors pops up without this
])
->setFinder($finder)
;
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"php": "^8.1",
"geocoder-php/plugin": "^1.5",
"php-http/discovery": "^1.14",
"symfony/console": "^5.4 || ^6.4 || ^7.0",
Expand All @@ -23,9 +23,8 @@
"willdurand/geocoder": "^4.6|^5.0"
},
"require-dev": {
"doctrine/annotations": "^1.11.1 || ^2.0",
"doctrine/doctrine-bundle": "^2.3",
"doctrine/orm": "^2.8 || ^3.0",
"doctrine/orm": "^2.20 || ^3.0",
"fakerphp/faker": "^1.20",
"friendsofphp/php-cs-fixer": "^3.13",
"geocoder-php/algolia-places-provider": "^0.4",
Expand Down Expand Up @@ -96,7 +95,7 @@
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
"dev-master": "6.0-dev"
}
}
}
82 changes: 13 additions & 69 deletions doc/doctrine.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@ First of all, update your entity:

```php

use Bazinga\GeocoderBundle\Mapping\Annotations as Geocoder;
use Bazinga\GeocoderBundle\Mapping\Attributes as Geocoder;

/**
* @Geocoder\Geocodeable
*/
#[Geocoder\Geocodeable()]
class User
{
/**
* @Geocoder\Address
*/
#[Geocoder\Address()]
private $address;

/**
* @Geocoder\Latitude
*/
#[Geocoder\Latitude()]
private $latitude;

/**
* @Geocoder\Longitude
*/
#[Geocoder\Longitude()]
private $longitude;
}
```
Expand All @@ -38,47 +30,36 @@ Instead of annotating a property, you can also annotate a getter:

```php

use Bazinga\GeocoderBundle\Mapping\Annotations as Geocoder;
use Bazinga\GeocoderBundle\Mapping\Attributes as Geocoder;

/**
* @Geocoder\Geocodeable
*/
#[Geocoder\Geocodeable()]
class User
{
/**
* @Geocoder\Latitude
*/
#[Geocoder\Latitude()]
private $latitude;

/**
* @Geocoder\Longitude
*/
#[Geocoder\Longitude()]
private $longitude;

/**
* @Geocoder\Address
*/
#[Geocoder\Address()]
public function getAddress(): string
{
// Your code...
}
}
```

Secondly, register the Doctrine event listener and its dependencies in your `config/services.yaml` file.
Secondly, register the Doctrine event listener and its dependencies in your `config/services.yaml` or `config/services.php` file.
You have to indicate which provider to use to reverse geocode the address. Here we use `acme` provider we declared in bazinga_geocoder configuration earlier.

```yaml
Bazinga\GeocoderBundle\Mapping\Driver\AnnotationDriver:
class: Bazinga\GeocoderBundle\Mapping\Driver\AnnotationDriver
arguments:
- '@annotations.reader'
Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver: ~

Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener:
class: Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener
arguments:
- '@bazinga_geocoder.provider.acme'
- '@Bazinga\GeocoderBundle\Mapping\Driver\AnnotationDriver'
- '@Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver'
tags:
- { name: doctrine.event_listener, event: onFlush }
```
Expand All @@ -96,40 +77,3 @@ $em->flush();
echo $user->getLatitude(); // will output 52.516325
echo $user->getLongitude(); // will output 13.377264
```

## PHP 8

If you are using PHP 8, you can use [Attributes](https://www.php.net/manual/en/language.attributes.overview.php) in your entity:

```php

use Bazinga\GeocoderBundle\Mapping\Annotations as Geocoder;

#[Geocoder\Geocodeable()]
class User
{
#[Geocoder\Address()]
private $address;

#[Geocoder\Latitude()]
private $latitude;

#[Geocoder\Longitude()]
private $longitude;
}
```

Then update your service configuration to register the `AttributeDriver`:

```yaml
Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver:
class: Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver

Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener:
class: Bazinga\GeocoderBundle\Doctrine\ORM\GeocoderListener
arguments:
- '@bazinga_geocoder.provider.acme'
- '@Bazinga\GeocoderBundle\Mapping\Driver\AttributeDriver'
tags:
- { name: doctrine.event_listener, event: onFlush }
```
18 changes: 0 additions & 18 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@
'count' => 1,
'path' => __DIR__.'/src/DependencyInjection/BazingaGeocoderExtension.php',
];
$ignoreErrors[] = [
// identifier: method.notFound
'message' => '#^Call to an undefined method Doctrine\\\\ORM\\\\Event\\\\OnFlushEventArgs\\:\\:getEntityManager\\(\\)\\.$#',
'count' => 1,
'path' => __DIR__.'/src/Doctrine/ORM/GeocoderListener.php',
];
$ignoreErrors[] = [
// identifier: method.nonObject
'message' => '#^Cannot call method getLatitude\\(\\) on Geocoder\\\\Model\\\\Coordinates\\|null\\.$#',
Expand All @@ -159,18 +153,6 @@
'count' => 1,
'path' => __DIR__.'/src/Doctrine/ORM/GeocoderListener.php',
];
$ignoreErrors[] = [
// identifier: missingType.generics
'message' => '#^Method Bazinga\\\\GeocoderBundle\\\\Mapping\\\\Driver\\\\AnnotationDriver\\:\\:getReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#',
'count' => 1,
'path' => __DIR__.'/src/Mapping/Driver/AnnotationDriver.php',
];
$ignoreErrors[] = [
// identifier: missingType.generics
'message' => '#^Method Bazinga\\\\GeocoderBundle\\\\Mapping\\\\Driver\\\\AttributeDriver\\:\\:getReflection\\(\\) return type with generic class ReflectionClass does not specify its types\\: T$#',
'count' => 1,
'path' => __DIR__.'/src/Mapping/Driver/AttributeDriver.php',
];
$ignoreErrors[] = [
// identifier: argument.type
'message' => '#^Parameter \\#1 \\$text of method Geocoder\\\\Query\\\\GeocodeQuery\\:\\:withText\\(\\) expects string, string\\|null given\\.$#',
Expand Down
42 changes: 14 additions & 28 deletions src/Doctrine/ORM/GeocoderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
*/
class GeocoderListener implements EventSubscriber
{
private DriverInterface $driver;
private Provider $geocoder;

public function __construct(Provider $geocoder, DriverInterface $driver)
{
$this->driver = $driver;
$this->geocoder = $geocoder;
public function __construct(
private readonly Provider $geocoder,
private readonly DriverInterface $driver,
) {
}

/**
Expand All @@ -45,20 +42,16 @@ public function getSubscribedEvents(): array
];
}

/**
* @return void
*/
public function onFlush(OnFlushEventArgs $args)
public function onFlush(OnFlushEventArgs $args): void
{
$em = method_exists($args, 'getObjectManager') ? $args->getObjectManager() : $args->getEntityManager();
$em = $args->getObjectManager();
$uow = $em->getUnitOfWork();

foreach ($uow->getScheduledEntityInsertions() as $entity) {
if (!$this->driver->isGeocodeable($entity)) {
continue;
}

/** @var ClassMetadata $metadata */
$metadata = $this->driver->loadMetadataFromObject($entity);

$this->geocodeEntity($metadata, $entity);
Expand All @@ -74,7 +67,6 @@ public function onFlush(OnFlushEventArgs $args)
continue;
}

/** @var ClassMetadata $metadata */
$metadata = $this->driver->loadMetadataFromObject($entity);

if (!$this->shouldGeocode($metadata, $uow, $entity)) {
Expand All @@ -90,17 +82,14 @@ public function onFlush(OnFlushEventArgs $args)
}
}

/**
* @param object $entity
*
* @return void
*/
private function geocodeEntity(ClassMetadata $metadata, $entity)
private function geocodeEntity(ClassMetadata $metadata, object $entity): void
{
if (null !== $metadata->addressGetter) {
$address = $metadata->addressGetter->invoke($entity);
} else {
} elseif (null !== $metadata->addressProperty) {
$address = $metadata->addressProperty->getValue($entity);
} else {
$address = '';
}

if (empty($address) || !is_string($address)) {
Expand All @@ -111,22 +100,19 @@ private function geocodeEntity(ClassMetadata $metadata, $entity)

if (!$results->isEmpty()) {
$result = $results->first();
$metadata->latitudeProperty->setValue($entity, $result->getCoordinates()->getLatitude());
$metadata->longitudeProperty->setValue($entity, $result->getCoordinates()->getLongitude());
$metadata->latitudeProperty?->setValue($entity, $result->getCoordinates()->getLatitude());
$metadata->longitudeProperty?->setValue($entity, $result->getCoordinates()->getLongitude());
}
}

/**
* @param object $entity
*/
private function shouldGeocode(ClassMetadata $metadata, UnitOfWork $unitOfWork, $entity): bool
private function shouldGeocode(ClassMetadata $metadata, UnitOfWork $unitOfWork, object $entity): bool
{
if (null !== $metadata->addressGetter) {
return true;
}

$changeSet = $unitOfWork->getEntityChangeSet($entity);

return isset($changeSet[$metadata->addressProperty->getName()]);
return isset($changeSet[$metadata->addressProperty?->getName() ?? '']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
* @license MIT License
*/

namespace Bazinga\GeocoderBundle\Mapping\Annotations;
namespace Bazinga\GeocoderBundle\Mapping\Attributes;

#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD)]
class Address
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
* @license MIT License
*/

namespace Bazinga\GeocoderBundle\Mapping\Annotations;
namespace Bazinga\GeocoderBundle\Mapping\Attributes;

#[\Attribute(\Attribute::TARGET_CLASS)]
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class Geocodeable
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
* @license MIT License
*/

namespace Bazinga\GeocoderBundle\Mapping\Annotations;
namespace Bazinga\GeocoderBundle\Mapping\Attributes;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Latitude
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
* @license MIT License
*/

namespace Bazinga\GeocoderBundle\Mapping\Annotations;
namespace Bazinga\GeocoderBundle\Mapping\Attributes;

#[\Attribute(\Attribute::TARGET_PROPERTY)]
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*
* @Annotation
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Longitude
{
}
Loading