kunicmarko20 /
SonataAnnotationBundle
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace KunicMarko\SonataAnnotationBundle\Reader; |
||
| 6 | |||
| 7 | use KunicMarko\SonataAnnotationBundle\Annotation\ShowAssociationField; |
||
| 8 | use KunicMarko\SonataAnnotationBundle\Annotation\ShowField; |
||
| 9 | use Sonata\AdminBundle\Show\ShowMapper; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @author Marko Kunic <[email protected]> |
||
| 13 | */ |
||
| 14 | final class ShowReader |
||
| 15 | { |
||
| 16 | use AnnotationReaderTrait; |
||
| 17 | |||
| 18 | public function configureFields(\ReflectionClass $class, ShowMapper $showMapper): void |
||
| 19 | { |
||
| 20 | $propertiesAndMethodsWithPosition = []; |
||
| 21 | $propertiesAndMethodsWithoutPosition = []; |
||
| 22 | |||
| 23 | // |
||
| 24 | // Properties |
||
| 25 | // |
||
| 26 | |||
| 27 | foreach ($class->getProperties() as $property) { |
||
| 28 | foreach ($this->getPropertyAnnotations($property) as $annotation) { |
||
| 29 | if (!$annotation instanceof ShowField && !$annotation instanceof ShowAssociationField) { |
||
| 30 | continue; |
||
| 31 | } |
||
| 32 | |||
| 33 | // the name property changes for ShowAssociationField |
||
| 34 | $name = $property->getName(); |
||
| 35 | if ($annotation instanceof ShowAssociationField) { |
||
| 36 | $name .= '.'.$annotation->getField(); |
||
| 37 | } |
||
| 38 | |||
| 39 | if (!$annotation->hasPosition()) { |
||
| 40 | $propertiesAndMethodsWithoutPosition[] = [ |
||
| 41 | 'name' => $name, |
||
| 42 | 'settings' => $annotation->getSettings(), |
||
| 43 | ]; |
||
| 44 | |||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) { |
||
| 49 | throw new \InvalidArgumentException(sprintf( |
||
| 50 | 'Position "%s" is already in use by "%s", try setting a different position for "%s".', |
||
| 51 | $annotation->position, |
||
| 52 | $propertiesAndMethodsWithPosition[$annotation->position]['name'], |
||
| 53 | $property->getName() |
||
| 54 | )); |
||
| 55 | } |
||
| 56 | |||
| 57 | $propertiesAndMethodsWithPosition[$annotation->position] = [ |
||
| 58 | 'name' => $name, |
||
| 59 | 'settings' => $annotation->getSettings(), |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | // |
||
| 65 | // Methods |
||
| 66 | // |
||
| 67 | |||
| 68 | foreach ($class->getMethods() as $method) { |
||
| 69 | if ($annotation = $this->getMethodAnnotation($method, ShowField::class)) { |
||
| 70 | $name = $method->getName(); |
||
| 71 | |||
| 72 | if (!$annotation->hasPosition()) { |
||
| 73 | $propertiesAndMethodsWithoutPosition[] = [ |
||
| 74 | 'name' => $name, |
||
| 75 | 'settings' => $annotation->getSettings(), |
||
| 76 | ]; |
||
| 77 | |||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (\array_key_exists($annotation->position, $propertiesAndMethodsWithPosition)) { |
||
| 82 | throw new \InvalidArgumentException(sprintf( |
||
| 83 | 'Position "%s" is already in use by "%s", try setting a different position for "%s".', |
||
| 84 | $annotation->position, |
||
| 85 | $propertiesAndMethodsWithPosition[$annotation->position]['name'], |
||
| 86 | $name |
||
| 87 | )); |
||
| 88 | } |
||
| 89 | |||
| 90 | $propertiesAndMethodsWithPosition[$annotation->position] = [ |
||
| 91 | 'name' => $name, |
||
| 92 | 'settings' => $annotation->getSettings(), |
||
| 93 | ]; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | // |
||
| 98 | // Sorting |
||
| 99 | // |
||
| 100 | |||
| 101 | \ksort($propertiesAndMethodsWithPosition); |
||
| 102 | |||
| 103 | $propertiesAndMethods = \array_merge($propertiesAndMethodsWithPosition, $propertiesAndMethodsWithoutPosition); |
||
| 104 | |||
| 105 | foreach ($propertiesAndMethods as $propertyAndMethod) { |
||
| 106 | $showMapper->add($propertyAndMethod['name'], ...$propertyAndMethod['settings']); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 |