vendor/sonata-project/doctrine-orm-admin-bundle/src/Admin/FieldDescription.php line 90

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\DoctrineORMAdminBundle\Admin;
  12. use Sonata\AdminBundle\Admin\BaseFieldDescription;
  13. class FieldDescription extends BaseFieldDescription
  14. {
  15.     public function __construct()
  16.     {
  17.         $this->parentAssociationMappings = [];
  18.     }
  19.     public function setAssociationMapping($associationMapping)
  20.     {
  21.         if (!\is_array($associationMapping)) {
  22.             throw new \RuntimeException('The association mapping must be an array');
  23.         }
  24.         $this->associationMapping $associationMapping;
  25.         $this->type $this->type ?: $associationMapping['type'];
  26.         $this->mappingType $this->mappingType ?: $associationMapping['type'];
  27.         $this->fieldName $associationMapping['fieldName'];
  28.     }
  29.     public function getTargetEntity()
  30.     {
  31.         if ($this->associationMapping) {
  32.             return $this->associationMapping['targetEntity'];
  33.         }
  34.     }
  35.     public function setFieldMapping($fieldMapping)
  36.     {
  37.         if (!\is_array($fieldMapping)) {
  38.             throw new \RuntimeException('The field mapping must be an array');
  39.         }
  40.         $this->fieldMapping $fieldMapping;
  41.         $this->type $this->type ?: $fieldMapping['type'];
  42.         $this->mappingType $this->mappingType ?: $fieldMapping['type'];
  43.         $this->fieldName $this->fieldName ?: $fieldMapping['fieldName'];
  44.     }
  45.     public function setParentAssociationMappings(array $parentAssociationMappings)
  46.     {
  47.         foreach ($parentAssociationMappings as $parentAssociationMapping) {
  48.             if (!\is_array($parentAssociationMapping)) {
  49.                 throw new \RuntimeException('An association mapping must be an array');
  50.             }
  51.         }
  52.         $this->parentAssociationMappings $parentAssociationMappings;
  53.     }
  54.     public function isIdentifier()
  55.     {
  56.         return isset($this->fieldMapping['id']) ? $this->fieldMapping['id'] : false;
  57.     }
  58.     public function getValue($object)
  59.     {
  60.         foreach ($this->parentAssociationMappings as $parentAssociationMapping) {
  61.             $object $this->getFieldValue($object$parentAssociationMapping['fieldName']);
  62.         }
  63.         $fieldMapping $this->getFieldMapping();
  64.         // Support embedded object for mapping
  65.         // Ref: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/embeddables.html
  66.         if (isset($fieldMapping['declaredField'])) {
  67.             $parentFields explode('.'$fieldMapping['declaredField']);
  68.             foreach ($parentFields as $parentField) {
  69.                 $object $this->getFieldValue($object$parentField);
  70.             }
  71.         }
  72.         return $this->getFieldValue($object$this->fieldName);
  73.     }
  74. }