-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathAnnotationDriver.php
87 lines (70 loc) · 2.9 KB
/
AnnotationDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
declare(strict_types=1);
/*
* This file is part of the BazingaGeocoderBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Bazinga\GeocoderBundle\Mapping\Driver;
use Bazinga\GeocoderBundle\Mapping\Annotations;
use Bazinga\GeocoderBundle\Mapping\ClassMetadata;
use Bazinga\GeocoderBundle\Mapping\Exception;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
/**
* @author Markus Bachmann <[email protected]>
*/
class AnnotationDriver implements DriverInterface
{
private Reader $reader;
public function __construct(Reader $reader)
{
$this->reader = $reader;
}
public function isGeocodeable($object): bool
{
$reflection = self::getReflection($object);
return (bool) $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class);
}
public function loadMetadataFromObject($object): ClassMetadata
{
$reflection = self::getReflection($object);
if (!$annotation = $this->reader->getClassAnnotation($reflection, Annotations\Geocodeable::class)) {
throw new Exception\MappingException(sprintf('The class %s is not geocodeable', get_class($object)));
}
$metadata = new ClassMetadata();
foreach ($reflection->getProperties() as $property) {
foreach ($this->reader->getPropertyAnnotations($property) as $annotation) {
if ($annotation instanceof Annotations\Latitude) {
$property->setAccessible(true);
$metadata->latitudeProperty = $property;
} elseif ($annotation instanceof Annotations\Longitude) {
$property->setAccessible(true);
$metadata->longitudeProperty = $property;
} elseif ($annotation instanceof Annotations\Address) {
$property->setAccessible(true);
$metadata->addressProperty = $property;
}
}
}
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if ($this->reader->getMethodAnnotation($method, Annotations\Address::class)) {
if (0 !== $method->getNumberOfRequiredParameters()) {
throw new \Exception('You can not use a method requiring parameters with @Address annotation!');
}
$metadata->addressGetter = $method;
}
}
return $metadata;
}
private static function getReflection(object $object): \ReflectionClass
{
if (class_exists(ClassUtils::class)) {
return ClassUtils::newReflectionObject($object);
}
return new \ReflectionClass(DefaultProxyClassNameResolver::getClass($object));
}
}