|
| 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\Mapping\Driver; |
| 14 | + |
| 15 | +use Bazinga\GeocoderBundle\Mapping\Annotations; |
| 16 | +use Bazinga\GeocoderBundle\Mapping\ClassMetadata; |
| 17 | +use Bazinga\GeocoderBundle\Mapping\Exception\MappingException; |
| 18 | +use Doctrine\Common\Util\ClassUtils; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Pierre du Plessis <[email protected]> |
| 22 | + */ |
| 23 | +final class AttributeDriver implements DriverInterface |
| 24 | +{ |
| 25 | + public function isGeocodeable($object): bool |
| 26 | + { |
| 27 | + if (PHP_VERSION_ID < 80000) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + $reflection = ClassUtils::newReflectionObject($object); |
| 32 | + |
| 33 | + return count($reflection->getAttributes(Annotations\Geocodeable::class)) > 0; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @throws MappingException |
| 38 | + */ |
| 39 | + public function loadMetadataFromObject($object): ClassMetadata |
| 40 | + { |
| 41 | + if (PHP_VERSION_ID < 80000) { |
| 42 | + throw new MappingException(sprintf('The class %s is not geocodeable', get_class($object))); |
| 43 | + } |
| 44 | + |
| 45 | + $reflection = ClassUtils::newReflectionObject($object); |
| 46 | + |
| 47 | + $attributes = $reflection->getAttributes(Annotations\Geocodeable::class); |
| 48 | + |
| 49 | + if (0 === count($attributes)) { |
| 50 | + throw new MappingException(sprintf('The class %s is not geocodeable', get_class($object))); |
| 51 | + } |
| 52 | + |
| 53 | + $metadata = new ClassMetadata(); |
| 54 | + |
| 55 | + foreach ($reflection->getProperties() as $property) { |
| 56 | + foreach ($property->getAttributes() as $attribute) { |
| 57 | + if (Annotations\Latitude::class === $attribute->getName()) { |
| 58 | + $property->setAccessible(true); |
| 59 | + $metadata->latitudeProperty = $property; |
| 60 | + } elseif (Annotations\Longitude::class === $attribute->getName()) { |
| 61 | + $property->setAccessible(true); |
| 62 | + $metadata->longitudeProperty = $property; |
| 63 | + } elseif (Annotations\Address::class === $attribute->getName()) { |
| 64 | + $property->setAccessible(true); |
| 65 | + $metadata->addressProperty = $property; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
| 71 | + if (count($method->getAttributes(Annotations\Address::class)) > 0) { |
| 72 | + if (0 !== $method->getNumberOfRequiredParameters()) { |
| 73 | + throw new MappingException('You can not use a method requiring parameters with #[Address] attribute!'); |
| 74 | + } |
| 75 | + |
| 76 | + $metadata->addressGetter = $method; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $metadata; |
| 81 | + } |
| 82 | +} |
0 commit comments