@param null $entity

@return array
188212
public function getFieldsChoices($entity = null)
    {
        $entityChoices = $this->getEntityChoices();

        $fieldsChoices = [];

        $translator = $this->getContainer()->get('translator');

        $adminPool = $this->getContainer()->get('sonata.admin.pool');

        $em = $this->getContainer()->get('doctrine')->getManager();

        foreach ($entityChoices as $entityChoiceKey => $entityChoice) {
            if ($entity && $entityChoice !== $entity) {
                continue;
            }

            $admin = $adminPool->getAdminByClass($entityChoice);

            if (!$admin) {
                throw new \RuntimeException('Not found admin by class: ' . $entityChoice);
            }

            $translation_domain = $admin->getTranslationDomain();

            $classMetadata = $em->getClassMetadata($entityChoice);

            /** @var array $associationMappings */
            $associationMappings = $classMetadata->associationMappings;

            /** @var array $fieldsMappings */
            $fieldsMappings = $classMetadata->fieldMappings;

            foreach ($fieldsMappings as $fieldsMapping) {
                $fieldsChoices[$fieldsMapping['fieldName']] =
                    $entityChoiceKey . ' / ' . $translator->trans('form.label_' . $fieldsMapping['columnName'], [], $translation_domain);
            }

            foreach ($associationMappings as $associationMapping) {
                $mappingFieldName = $associationMapping['fieldName'];
                $mappingAdmin = $adminPool->getAdminByClass($associationMapping['targetEntity']);

                if (!$mappingAdmin) {
                    continue;
                }

                $mappingTranslationDomain = $mappingAdmin->getTranslationDomain();

                $classMetadataMapping = $em->getClassMetadata($associationMapping['targetEntity']);

                /** @var array $fieldsMappingsMapping */
                $fieldsMappingsMapping = $classMetadataMapping->fieldMappings;

                foreach ($fieldsMappingsMapping as $fieldsMappingAssoc) {
                    $fieldsChoices[$mappingFieldName . '.' . $fieldsMappingAssoc['fieldName']] =
                        $entityChoiceKey . ' / '
                        . $translator->trans('form.label_' . $this->camelCaseToUnderscore($mappingFieldName), [], $translation_domain) . ' / '
                        . $translator->trans('form.label_' . $this->camelCaseToUnderscore($fieldsMappingAssoc['fieldName']), [], $mappingTranslationDomain);
                }
            }
        }

        return $fieldsChoices;
    }
991110
public function getAttributes($entity)
    {
        if ($entity->has($this->idField)) {
            $hidden = array_merge($entity->getHidden(), [$this->idField]);
            $entity->setHidden($hidden);
        }

        $attributes = $entity->toArray();

        // remove associated data so it won't appear inside jsonapi `attributes`
        foreach ($this->_repository->associations() as $association) {
            $propertyName = $association->getProperty();

            if ($association->type() === Association::MANY_TO_ONE) {
                $foreignKey = $association->getForeignKey();
                unset($attributes[$foreignKey]);
            }

            unset($attributes[$propertyName]);
        }

        // dasherize attribute keys (like `created_by`) if need be
        if ($this->_view->viewVars['_inflect'] === 'dasherize') {
            foreach ($attributes as $key => $value) {
                $dasherizedKey = Inflector::dasherize($key);

                if (!array_key_exists($dasherizedKey, $attributes)) {
                    $attributes[$dasherizedKey] = $value;
                    unset($attributes[$key]);
                }
            }
        }

        return $attributes;
    }
763407
public function duplicate(Entity $entity = null)
    {
        $entity = $this->getEntityIfEmpty($entity);

        $data = $this->toArray();
        if (isset($data['id'])) {
            unset($data['id']);
        }

        foreach ($data as $key => &$val) {
            if ($key && array_key_exists($key . '_x', $data) && array_key_exists($key . '_y', $data)) {
                $val = [
                    $data[$key . '_x'],
                    $data[$key . '_y'],
                ];
            }
        }

        $record = new static($data);
        $record->save($entity);

        return $record;
    }
965895
public function serialize($entity)
    {
        return array($this->getName().'.'.$this->_target_parent_key => $entity->get($this->_target_parent_key));
    }
257273
public function generateMethods(Entity $entity)
    {
        $methods    = [];
        $descriptor = $entity->getDescriptor();
        $values     = $descriptor->getValueList() ?: [];

        foreach ($values as $value) {
            $methods[] = $this->generateMethod($entity, $value);
        }

        $methods[] = $this->generateValueOfMethod($entity);

        return $methods;
    }
535096
protected function getIdentifier($entity)
    {
        $entityId = json_decode($this->entityRegistry->getEntityId($entity), true);

        if (is_array($entityId)) {
            $identifier = [];

            foreach ($entityId as $key => $value) {
                $key              = strtolower(Normalise::toUnderscoreSeparated(Normalise::fromCamelCase($key)));
                $identifier[$key] = $value;
            }

            return $identifier;
        }

        $primary = $this->builder->getMeta(get_class($entity))->primary;

        return [$primary => $entityId];
    }
24317
protected function getConditions(EntityInterface $entity)
    {
        $parts = explode('\\', $this->getEntityDescriptor()->className());
        $property = lcfirst(array_pop($parts));
        return [
            "rel.{$this->getForeignKey()} = :{$property}" => [
                ":{$property}" => $entity->getId()
            ]
        ];
    }
254539
public function getEntityChangeSet($entity)
    {
        $oid = spl_object_hash($entity);

        if (isset($this->entityChangeSets[$oid])) {
            return $this->entityChangeSets[$oid];
        }

        return array();
    }
194278
public static function getUploadHeaders(ImportEntity $entity) : array
    {
        $reader = Reader::createFromPath($entity->filename, 'r');

        return $reader->fetchOne();
    }
954737
protected function generateFields(Entity $entity)
    {
        $properties = [];
        $descriptor = $entity->getDescriptor();
        $fields     = $descriptor->getFieldList() ?: [];
        $extensions = $descriptor->getExtensionList() ?: [];

        $properties[] = PropertyGenerator::fromArray([
            'name'       => $this->getUniqueFieldName($descriptor, 'unknownFieldSet'),
            'visibility' => PropertyGenerator::VISIBILITY_PROTECTED,
            'docblock'   => [
                'tags'   => [
                    [
                        'name'        => 'var',
                        'description' => '\Protobuf\UnknownFieldSet',
                    ]
                ]
            ]
        ]);

        $properties[] = PropertyGenerator::fromArray([
            'name'       => $this->getUniqueFieldName($descriptor, 'extensions'),
            'visibility' => PropertyGenerator::VISIBILITY_PROTECTED,
            'docblock'   => [
                'tags'   => [
                    [
                        'name'        => 'var',
                        'description' => '\Protobuf\Extension\ExtensionFieldMap',
                    ]
                ]
            ]
        ]);

        foreach ($fields as $field) {
            $properties[] = $this->generateField($entity, $field);
        }

        return $properties;
    }
137459
private function getEntityIdentifierValue($entity)
    {
        $entityMetaData = $this->entityManager->getEntityMetadataRegister()->getEntityMetadata($entity);

        return $entityMetaData ? $entityMetaData->getIdentifierValue() : null;
    }
862456
public function transform($entity)
    {
        $data = array();
        if (empty($entity)) {
            return $data;
        }

        $text = is_null($this->textProperty)
            ? (string) $entity
            : $this->accessor->getValue($entity, $this->textProperty);

        if ($this->em->contains($entity)) {
            $value = (string) $this->accessor->getValue($entity, $this->primaryKey);
        } else {
            $value = $this->newTagPrefix . $text;
            $text = $text.$this->newTagText;
        }

        $data[$value] = $text;

        return $data;
    }
104292
public function getMetaData($entity)
    {
        $propertyAnnotations = [];
        $result = new \stdClass;
        $class = new \ReflectionClass($entity);
        $classAnnotations = $this->_reader->getClassAnnotations($class);
        foreach ($class->getProperties() as $key => $value) {
            $annotation = $this->_reader->getPropertyAnnotations(new \ReflectionProperty($entity, $value->name));
            if (count($annotation)) {
                $propertyAnnotations[$value->name] = array_pop($annotation);
            }
        }

        foreach ($classAnnotations as $annotation) {
            if ($annotation instanceof \MercadoPago\Annotation\RestMethod) {
                $result->methods[$annotation->method] = get_object_vars($annotation);
            }
            if ($annotation instanceof \MercadoPago\Annotation\RequestParam) {
                $result->params[] = $annotation->param;
            }
            if ($annotation instanceof \MercadoPago\Annotation\DenyDynamicAttribute) {
                $result->denyDynamicAttribute = true;
            }
        }

        foreach ($propertyAnnotations as $key => $annotation) {
            if ($annotation instanceof \MercadoPago\Annotation\Attribute) {
                $result->attributes[$key] = get_object_vars($annotation);
            }
        }

        return $result;
    }
832517
protected function _fieldsAreNull($entity, $source)
    {
        $nulls = 0;
        $schema = $source->getSchema();
        foreach ($this->_fields as $field) {
            if ($schema->getColumn($field) && $schema->isNullable($field) && $entity->get($field) === null) {
                $nulls++;
            }
        }

        return $nulls === count($this->_fields);
    }
989527
private function getPropertiesByType($entity, $type)
    {
        $properties = $this->readClassProperties($entity);

        $fields = [];
        foreach ($properties as $property) {
            $annotation = $this->reader->getPropertyAnnotation($property, $type);

            if (null === $annotation) {
                continue;
            }

            $property->setAccessible(true);
            $annotation->value = $property->getValue($entity);
            $annotation->name = $property->getName();

            $fields[] = $annotation;
        }

        return $fields;
    }
900681
protected function singleEntity($entity)
    {
        if (empty($entity)) {
            return null;
        }

        $singleEntity = $entity;

        if (is_array($entity)) {
            if (count($entity) > 1) {
                throw RuntimeException::multipleResultsFound();
            }

            $singleEntity = reset($entity);
        }

        $entityClass = $this->getEntityName();

        if (!$singleEntity instanceof $entityClass) {
            throw InvalidArgumentException::invalidEntityType(
                $this->getEntityName(),
                $singleEntity
            );
        }

        return $singleEntity;
    }
507221
private function getMeta($entity)
    {
        $parts  = explode('\\', $entity);
        $entity = array_pop($parts);

        if ($entity[0] == '@') {
            // Indirect entity definition; skip
            return null;
        }

        if (!isset($this->entityDefinitions[$entity])) {
            // Unknown entity definition; skip
            return null;
        }

        return $this->entityDefinitions[$entity];
    }