/**
* @param Property $property
* @param Relationship\BelongsToOne $relationship micro-optimalization
* @param Filtering|null $filtering
* @return Entity
* @throws InvalidValueException
*/
private function getBelongsToOneValue(Property $property, Relationship\BelongsToOne $relationship, Filtering $filtering = null)
{
$targetTable = $relationship->getTargetTable();
$rows = $this->row->referencing($targetTable, $relationship->getColumnReferencingSourceTable(), $filtering, $relationship->getStrategy());
$count = count($rows);
if ($count > 1) {
throw new InvalidValueException('There cannot be more than one entity referencing to entity ' . get_called_class() . " in property '{$property->getName()}' with m:belongToOne relationship.");
} elseif ($count === 0) {
if (!$property->isNullable()) {
$name = $property->getName();
throw new InvalidValueException("Property '{$name}' cannot be null in entity " . get_called_class() . '.');
}
return null;
} else {
$row = reset($rows);
$entityClass = $this->mapper->getEntityClass($targetTable, $row);
$entity = $this->entityFactory->createEntity($entityClass, $row);
$this->checkConsistency($property, $entityClass, $entity);
$entity->makeAlive($this->entityFactory);
return $entity;
}
}