/** * @param string $sourceClass * @param PropertyType $propertyType * @param string $relationshipType * @param string|null $definition * @param IMapper|null $mapper * @return mixed * @throws InvalidAnnotationException */ private static function createRelationship($sourceClass, PropertyType $propertyType, $relationshipType, $definition = null, IMapper $mapper = null) { if ($relationshipType !== 'hasOne') { $strategy = Result::STRATEGY_IN; // default strategy if ($definition !== null and substr($definition, -6) === '#union') { $strategy = Result::STRATEGY_UNION; $definition = substr($definition, 0, -6); } } $pieces = array_replace(array_fill(0, 6, ''), $definition !== null ? explode(':', $definition) : []); $sourceTable = $mapper !== null ? $mapper->getTable($sourceClass) : null; $targetTable = $mapper !== null ? $mapper->getTable($propertyType->getType()) : null; switch ($relationshipType) { case 'hasOne': $relationshipColumn = $mapper !== null ? $mapper->getRelationshipColumn($sourceTable, $targetTable) : self::getSurrogateRelationshipColumn($propertyType); return new Relationship\HasOne($pieces[0] ?: $relationshipColumn, $pieces[1] ?: $targetTable); case 'hasMany': return new Relationship\HasMany($pieces[0] ?: ($mapper !== null ? $mapper->getRelationshipColumn($mapper->getRelationshipTable($sourceTable, $targetTable), $sourceTable) : null), $pieces[1] ?: ($mapper !== null ? $mapper->getRelationshipTable($sourceTable, $targetTable) : null), $pieces[2] ?: ($mapper !== null ? $mapper->getRelationshipColumn($mapper->getRelationshipTable($sourceTable, $targetTable), $targetTable) : null), $pieces[3] ?: $targetTable, $strategy); case 'belongsToOne': $relationshipColumn = $mapper !== null ? $mapper->getRelationshipColumn($targetTable, $sourceTable) : $sourceTable; return new Relationship\BelongsToOne($pieces[0] ?: $relationshipColumn, $pieces[1] ?: $targetTable, $strategy); case 'belongsToMany': $relationshipColumn = $mapper !== null ? $mapper->getRelationshipColumn($targetTable, $sourceTable) : $sourceTable; return new Relationship\BelongsToMany($pieces[0] ?: $relationshipColumn, $pieces[1] ?: $targetTable, $strategy); } return null; }