Returns the value of an entity using reflection

@param mixed $entity
@param \ReflectionProperty $reflectionProperty

@return mixed
427174
private function getFieldValue($entity, \ReflectionProperty $reflectionProperty)
    {
        $value = null;
        if ($reflectionProperty) {
            $reflectionProperty->setAccessible(true);
            $value = $reflectionProperty->getValue($entity);
        }

        return $value;
    }
1035666
public function getValue($entity)
    {
        $meta = $this->metadataManager->getMetadata($entity);
        $identifier = $meta->getIdentifier();

        if (!$this->propertyAccessor->isReadable($entity, $identifier)) {
            throw new MetadataException(
                sprintf('Identifier "%s::$%s" is not readable.', ClassUtils::getClass($entity), $identifier)
            );
        }

        return $this->propertyAccessor->getValue($entity, $identifier);
    }
216641
public function getAdminForEntity($entity): Admin
    {
        $default = null;
        foreach ($this->adminRegistry->getAdmins() as $admin) {
            if (is_a($entity, $admin->getEntity())) {
                if ($entity instanceof DataInterface) {
                    $default = $default ?: $admin;
                    /** @var array $families */
                    $families = $admin->getOption('families', []);
                    foreach ($families as $family => $config) {
                        if ($entity->getFamilyCode() === $family) {
                            return $admin;
                        }
                    }
                } else {
                    return $admin;
                }
            }
        }

        if ($default) {
            // Or throw exception anyway ?
            return $default;
        }
        $class = \get_class($entity);
        throw new \UnexpectedValueException("No admin matching for entity {$class}");
    }
834282
private function findProperty(\ReflectionClass $ref, $field)
    {
        while (!$ref->hasProperty($field)) {
            $ref = $ref->getParentClass();
        }

        return $ref->getProperty($field);
    }
940206
private function readClassProperties($entity)
    {
        $className = get_class($entity);
        if (isset($this->entityProperties[$className])) {
            return $this->entityProperties[$className];
        }

        $reflectionClass = new \ReflectionClass($entity);
        $inheritedProperties = array_merge($this->getParentProperties($reflectionClass), $reflectionClass->getProperties());

        $properties = [];
        foreach ($inheritedProperties as $property) {
            $properties[$property->getName()] = $property;
        }

        $this->entityProperties[$className] = $properties;

        return $properties;
    }
44613
protected static function createProperty(\ReflectionProperty $reflectionProperty)
    {
        $property = new Property();
        $property->setName($reflectionProperty->getName());
        
        $annotationReader = new AnnotationReader();
        $annotations = $annotationReader->getPropertyAnnotations($reflectionProperty);
        foreach ($annotations as $annotation) {
            $annotationClassName = get_class($annotation);
            if ($annotationClassName == 'Doctrine\ORM\Mapping\Column' || $annotationClassName == 'Doctrine\ORM\Mapping\Id') {
                $property->setAnnotation($annotation);
                $property->setType(Property::PROPERTY_TYPE_COLUMN);
            } elseif ($annotationClassName == 'Doctrine\ORM\Mapping\ManyToOne' || $annotationClassName == 'Doctrine\ORM\Mapping\OneToOne') {
                $property->setAnnotation($annotation);
                $property->setType(Property::PROPERTY_TYPE_REF_ONE);
                $property->setTargetEntity($annotation->targetEntity);
            } elseif ($annotationClassName == 'Doctrine\ORM\Mapping\ManyToMany' || $annotationClassName == 'Doctrine\ORM\Mapping\OneToMany') {
                $property->setAnnotation($annotation);
                $property->setType(Property::PROPERTY_TYPE_REF_MANY);
                $property->setTargetEntity($annotation->targetEntity);
            }
        }
        
        if ($property->getType() == -1) {
            throw new \Exception(
                'Entity "' . $reflectionProperty->getDeclaringClass()->getName()
                . '": defining annotation is missing at property "' . $property->getName() . '"!'
            );
        }
        
        return $property;
    }
1334459
public function getPropertyClass(ReflectionProperty $property)
    {
        // Get the content of the @var annotation
        if (preg_match('/@var\s+([^\s]+)/', $property->getDocComment(), $matches)) {
            list(, $type) = $matches;
        } else {
            return null;
        }

        // Ignore primitive types
        if (in_array($type, $this->ignoredTypes)) {
            return null;
        }

        // Ignore types containing special characters ([], <> ...)
        if (! preg_match('/^[a-zA-Z0-9\\\\_]+$/', $type)) {
            return null;
        }

        $class = $property->getDeclaringClass();

        // If the class name is not fully qualified (i.e. doesn't start with a \)
        if ($type[0] !== '\\') {
            // Try to resolve the FQN using the class context
            $resolvedType = $this->tryResolveFqn($type, $class, $property);

            if (!$resolvedType && !$this->ignorePhpDocErrors) {
                throw new AnnotationException(sprintf(
                    'The @var annotation on %s::%s contains a non existent class "%s". '
                        . 'Did you maybe forget to add a "use" statement for this annotation?',
                    $class->name,
                    $property->getName(),
                    $type
                ));
            }
            
            $type = $resolvedType;
        }

        if (!$this->classExists($type) && !$this->ignorePhpDocErrors) {
            throw new AnnotationException(sprintf(
                'The @var annotation on %s::%s contains a non existent class "%s"',
                $class->name,
                $property->getName(),
                $type
            ));
        }

        // Remove the leading \ (FQN shouldn't contain it)
        $type = ltrim($type, '\\');

        return $type;
    }
922807
public function all($entity)
    {
        $reflectionClass = new \ReflectionClass($entity);
        $properties = $reflectionClass->getProperties();

        $class = new \ReflectionClass($entity);

        while ($parent = $class->getParentClass()) {
            $parentProperties = $parent->getProperties();
            $properties = array_merge($parentProperties, $properties);
            $class = $parent;
        }

        $return = array();
        foreach ($properties as $reflectionProperty) {
            $propertyAnnotation = $this->reader->getPropertyAnnotation($reflectionProperty, $this->annotationClass);
            if (!is_null($propertyAnnotation) && $propertyAnnotation->listable) {
                $return[] = array(
                    'property' => $reflectionProperty->name,
                    'type' => $propertyAnnotation->type
                );
            }
        }

        return $return;
    }
729791
public function fetchId($entity) {
        $id = null;
        $idProperty = $this->fetchIdProperty(new \ReflectionClass($entity));
        if ($idProperty) {
            $idProperty->setAccessible(TRUE);
            $id = $idProperty->getValue($entity);
        }
        return $id;
    }
1004778
public function getRepository(Entity $entity)
    {
        $repo = $this->getRepositories()[get_class($entity)];

        return $this->$repo;
    }
576065
private function getPropertyValue(array $property, $className = Entity::class)
    {
        $type = $this->getValueType($property);
        return $this->convertValue($type, $property[$type], $className);
    }
466297
public function get($entity)
    {
        $key = $this->mapping['fieldname'];
        $valueMethod = 'get' . ucfirst($key);
        $value = $entity->$valueMethod();

        return $value;
    }
1077772
public function save(Entity $entity)
    {
        $requiredClass = Convention::nameToClass($this->getEntityName(), Convention::ENTITY_MASK);
        if (!$entity instanceof $requiredClass) {
            throw new Exception\RepositoryException(
                "Entity must be instance of ". $requiredClass . "!"
            );
        }

        $reflection = $entity::getReflection();
        if (!$reflection->hasPrimary()) {
            throw new Exception\RepositoryException(
                "Can not save entity without primary property!"
            );
        }

        $primaryName = $reflection->getPrimaryProperty()->getName();
        $primaryValue = $entity->{$primaryName};

        if ($primaryValue === null) {

            $primaryValue = $this->create($entity);
            if (Entity\Reflection\Property\Option\Primary::isEmpty($primaryValue)) {
                throw new RepositoryException(
                    "Entity was successfully created but returned primary is empty!"
                );
            }
            $entity->{$primaryName} = $primaryValue;
        } else {
            $this->update($entity, $primaryValue);
        }

        return $entity;
    }
949892
public function getEntity($entity = null)
    {
        if ($entity === null) {
            $class = $this->class;
            $entity = new $class();
        }

        if ($entity instanceof Content && !$entity->getContenttype() && $ct = $this->getClassMetadata()->getBoltName()) {
            $contentType = $this->metadata->createContentType($ct);
            if ($contentType) {
                $entity->setContenttype($contentType);
            } else {
                $entity->setContenttype($ct);
            }
        }

        return $entity;
    }
919973
private function extractIdentity(object $entity): Identity
    {
        $identity = ($this->metadata)(get_class($entity))->identity()->property();

        return ReflectionObject::of($entity)
            ->extract($identity)
            ->get($identity);
    }
447124
public function getId(&$entity)
    {
        $structure = $this->getEntityStructure($entity);

        if (isset($entity->{$structure->primaryColumn->propertyName})) {
            return $entity->{$structure->primaryColumn->propertyName};
        }
        return null; // @codeCoverageIgnore
    }
499841
protected function getEntityAttributeValue($entity, $field)
    {
        $functionName = 'get'.ucfirst($field);

        $fieldValue = call_user_func([$entity, $functionName]);

        return $fieldValue;
    }