Skip to content

Commit

Permalink
Merge pull request #9758 from greg0ire/native-types-ns
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire authored May 17, 2022
2 parents 72810e9 + 98cf583 commit c8735de
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 120 deletions.
12 changes: 6 additions & 6 deletions docs/en/reference/namingstrategy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,28 @@ You need to create a class which implements ``Doctrine\ORM\Mapping\NamingStrateg
<?php
class MyAppNamingStrategy implements NamingStrategy
{
public function classToTableName($className)
public function classToTableName(string $className): string
{
return 'MyApp_' . substr($className, strrpos($className, '\\') + 1);
}
public function propertyToColumnName($propertyName)
public function propertyToColumnName(string $propertyName): string
{
return $propertyName;
}
public function referenceColumnName()
public function referenceColumnName(): string
{
return 'id';
}
public function joinColumnName($propertyName, $className = null)
public function joinColumnName(string $propertyName, ?string $className = null): string
{
return $propertyName . '_' . $this->referenceColumnName();
}
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
public function joinTableName(string $sourceEntity, string $targetEntity, string $propertyName): string
{
return strtolower($this->classToTableName($sourceEntity) . '_' .
$this->classToTableName($targetEntity));
}
public function joinKeyColumnName($entityName, $referencedColumnName = null)
public function joinKeyColumnName(string $entityName, ?string $referencedColumnName): string
{
return strtolower($this->classToTableName($entityName) . '_' .
($referencedColumnName ?: $this->referenceColumnName()));
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3325,6 +3325,8 @@ public function inlineEmbeddable($property, ClassMetadataInfo $embeddable)
if (! empty($this->embeddedClasses[$property]['columnPrefix'])) {
$fieldMapping['columnName'] = $this->embeddedClasses[$property]['columnPrefix'] . $fieldMapping['columnName'];
} elseif ($this->embeddedClasses[$property]['columnPrefix'] !== false) {
assert($this->reflClass !== null);
assert($embeddable->reflClass !== null);
$fieldMapping['columnName'] = $this->namingStrategy
->embeddedFieldToColumnName(
$property,
Expand Down
48 changes: 19 additions & 29 deletions lib/Doctrine/ORM/Mapping/DefaultNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
*/
class DefaultNamingStrategy implements NamingStrategy
{
/**
* {@inheritdoc}
*/
public function classToTableName($className)
public function classToTableName(string $className): string
{
if (str_contains($className, '\\')) {
return substr($className, strrpos($className, '\\') + 1);
Expand All @@ -28,55 +25,48 @@ public function classToTableName($className)
return $className;
}

/**
* {@inheritdoc}
*/
public function propertyToColumnName($propertyName, $className = null)
public function propertyToColumnName(string $propertyName, string $className): string
{
return $propertyName;
}

/**
* {@inheritdoc}
*/
public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
{
public function embeddedFieldToColumnName(
string $propertyName,
string $embeddedColumnName,
string $className,
string $embeddedClassName,
): string {
return $propertyName . '_' . $embeddedColumnName;
}

/**
* {@inheritdoc}
*/
public function referenceColumnName()
public function referenceColumnName(): string
{
return 'id';
}

/**
* {@inheritdoc}
*
* @param string $propertyName
* @param class-string $className
*/
public function joinColumnName($propertyName, $className = null)
public function joinColumnName(string $propertyName, ?string $className = null): string
{
return $propertyName . '_' . $this->referenceColumnName();
}

/**
* {@inheritdoc}
*/
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
{
public function joinTableName(
string $sourceEntity,
string $targetEntity,
string $propertyName,
): string {
return strtolower($this->classToTableName($sourceEntity) . '_' .
$this->classToTableName($targetEntity));
}

/**
* {@inheritdoc}
*/
public function joinKeyColumnName($entityName, $referencedColumnName = null)
{
public function joinKeyColumnName(
string $entityName,
?string $referencedColumnName,
): string {
return strtolower($this->classToTableName($entityName) . '_' .
($referencedColumnName ?: $this->referenceColumnName()));
}
Expand Down
48 changes: 14 additions & 34 deletions lib/Doctrine/ORM/Mapping/NamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,46 @@ interface NamingStrategy
* Returns a table name for an entity class.
*
* @param class-string $className
*
* @return string A table name.
*/
public function classToTableName($className);
public function classToTableName(string $className): string;

/**
* Returns a column name for a property.
*
* @param string $propertyName A property name.
* @param class-string $className The fully-qualified class name.
*
* @return string A column name.
* @param class-string $className
*/
public function propertyToColumnName($propertyName, $className = null);
public function propertyToColumnName(string $propertyName, string $className): string;

/**
* Returns a column name for an embedded property.
*
* @param string $propertyName
* @param string $embeddedColumnName
* @param class-string $className
* @param class-string $embeddedClassName
*
* @return string
*/
public function embeddedFieldToColumnName(
$propertyName,
$embeddedColumnName,
$className = null,
$embeddedClassName = null
);
string $propertyName,
string $embeddedColumnName,
string $className,
string $embeddedClassName,
): string;

/**
* Returns the default reference column name.
*
* @return string A column name.
*/
public function referenceColumnName();
public function referenceColumnName(): string;

/**
* Returns a join column name for a property.
*
* @param string $propertyName A property name.
*
* @return string A join column name.
*/
public function joinColumnName($propertyName/*, $className = null */);
public function joinColumnName(string $propertyName/*, $className = null */): string;

/**
* Returns a join table name.
*
* @param class-string $sourceEntity The source entity.
* @param class-string $targetEntity The target entity.
* @param string $propertyName A property name.
*
* @return string A join table name.
* @param class-string $sourceEntity
* @param class-string $targetEntity
*/
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null);
public function joinTableName(string $sourceEntity, string $targetEntity, string $propertyName): string;

/**
* Returns the foreign key column name for the given parameters.
Expand All @@ -82,8 +64,6 @@ public function joinTableName($sourceEntity, $targetEntity, $propertyName = null
* case of a self-referencing
* entity with join columns
* defined in the mapping
*
* @return string A join column name.
*/
public function joinKeyColumnName($entityName, $referencedColumnName = null);
public function joinKeyColumnName(string $entityName, ?string $referencedColumnName): string;
}
61 changes: 23 additions & 38 deletions lib/Doctrine/ORM/Mapping/UnderscoreNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,36 @@
*/
class UnderscoreNamingStrategy implements NamingStrategy
{
/** @var int */
private $case;
private int $case;

/**
* Underscore naming strategy construct.
*
* @param int $case CASE_LOWER | CASE_UPPER
*/
public function __construct($case = CASE_LOWER)
public function __construct(int $case = CASE_LOWER)
{
$this->case = $case;
}

/**
* @return int CASE_LOWER | CASE_UPPER
*/
public function getCase()
public function getCase(): int
{
return $this->case;
}

/**
* Sets string case CASE_LOWER | CASE_UPPER.
* Alphabetic characters converted to lowercase or uppercase.
*
* @param int $case
*
* @return void
*/
public function setCase($case)
public function setCase(int $case): void
{
$this->case = $case;
}

/**
* {@inheritdoc}
*/
public function classToTableName($className)
public function classToTableName(string $className): string
{
if (str_contains($className, '\\')) {
$className = substr($className, strrpos($className, '\\') + 1);
Expand All @@ -68,54 +60,47 @@ public function classToTableName($className)
return $this->underscore($className);
}

/**
* {@inheritdoc}
*/
public function propertyToColumnName($propertyName, $className = null)
public function propertyToColumnName(string $propertyName, string $className): string
{
return $this->underscore($propertyName);
}

/**
* {@inheritdoc}
*/
public function embeddedFieldToColumnName($propertyName, $embeddedColumnName, $className = null, $embeddedClassName = null)
{
public function embeddedFieldToColumnName(
string $propertyName,
string $embeddedColumnName,
string $className,
string $embeddedClassName,
): string {
return $this->underscore($propertyName) . '_' . $embeddedColumnName;
}

/**
* {@inheritdoc}
*/
public function referenceColumnName()
public function referenceColumnName(): string
{
return $this->case === CASE_UPPER ? 'ID' : 'id';
}

/**
* {@inheritdoc}
*
* @param string $propertyName
* @param class-string $className
*/
public function joinColumnName($propertyName, $className = null)
public function joinColumnName(string $propertyName, ?string $className = null): string
{
return $this->underscore($propertyName) . '_' . $this->referenceColumnName();
}

/**
* {@inheritdoc}
*/
public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
{
public function joinTableName(
string $sourceEntity,
string $targetEntity,
string $propertyName,
): string {
return $this->classToTableName($sourceEntity) . '_' . $this->classToTableName($targetEntity);
}

/**
* {@inheritdoc}
*/
public function joinKeyColumnName($entityName, $referencedColumnName = null)
{
public function joinKeyColumnName(
string $entityName,
?string $referencedColumnName
): string {
return $this->classToTableName($entityName) . '_' .
($referencedColumnName ?: $this->referenceColumnName());
}
Expand Down
10 changes: 2 additions & 8 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1128,10 +1128,7 @@ class DDC2700MappedSuperClass

class MyNamespacedNamingStrategy extends DefaultNamingStrategy
{
/**
* {@inheritdoc}
*/
public function classToTableName($className)
public function classToTableName(string $className): string
{
if (str_contains($className, '\\')) {
$className = str_replace('\\', '_', str_replace('Doctrine\Tests\Models\\', '', $className));
Expand All @@ -1143,10 +1140,7 @@ public function classToTableName($className)

class MyPrefixNamingStrategy extends DefaultNamingStrategy
{
/**
* {@inheritdoc}
*/
public function propertyToColumnName($propertyName, $className = null)
public function propertyToColumnName(string $propertyName, string $className): string
{
return strtolower($this->classToTableName($className)) . '_' . $propertyName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
*/
class JoinColumnClassNamingStrategy extends DefaultNamingStrategy
{
/**
* {@inheritdoc}
*/
public function joinColumnName($propertyName, $className = null)
public function joinColumnName(string $propertyName, ?string $className = null): string
{
return strtolower($this->classToTableName($className))
. '_' . $propertyName
Expand Down
2 changes: 1 addition & 1 deletion tests/Doctrine/Tests/ORM/Mapping/NamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public function testJoinColumnName(
/**
* Data Provider for NamingStrategy#joinTableName
*
* @return array<array{NamingStrategy, string, string, string|null}>
* @return array<array{NamingStrategy, string, string, string}>
*/
public static function dataJoinTableName(): array
{
Expand Down

0 comments on commit c8735de

Please sign in to comment.