-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract discriminator column mapping into its own DTO (#10609)
- Loading branch information
Showing
11 changed files
with
176 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Mapping; | ||
|
||
use ArrayAccess; | ||
use BackedEnum; | ||
use Exception; | ||
|
||
use function in_array; | ||
use function property_exists; | ||
|
||
/** @template-implements ArrayAccess<string, mixed> */ | ||
final class DiscriminatorColumnMapping implements ArrayAccess | ||
{ | ||
use ArrayAccessImplementation; | ||
|
||
/** The database length of the column. Optional. Default value taken from the type. */ | ||
public int|null $length = null; | ||
|
||
public string|null $columnDefinition = null; | ||
|
||
/** @var class-string<BackedEnum>|null */ | ||
public string|null $enumType = null; | ||
|
||
/** @var array<string, mixed> */ | ||
public array $options = []; | ||
|
||
public function __construct( | ||
public string $type, | ||
public string $fieldName, | ||
public string $name, | ||
) { | ||
} | ||
|
||
/** | ||
* @psalm-param array{ | ||
* type: string, | ||
* fieldName: string, | ||
* name: string, | ||
* length?: int, | ||
* columnDefinition?: string, | ||
* enumType?: class-string<BackedEnum>, | ||
* options?: array<string, mixed>, | ||
* } $mappingArray | ||
*/ | ||
public static function fromMappingArray(array $mappingArray): self | ||
{ | ||
$mapping = new self( | ||
$mappingArray['type'], | ||
$mappingArray['fieldName'], | ||
$mappingArray['name'], | ||
); | ||
foreach ($mappingArray as $key => $value) { | ||
if (in_array($key, ['type', 'fieldName', 'name'])) { | ||
continue; | ||
} | ||
|
||
if (property_exists($mapping, $key)) { | ||
$mapping->$key = $value; | ||
} else { | ||
throw new Exception('Unknown property ' . $key . ' on class ' . static::class); | ||
} | ||
} | ||
|
||
return $mapping; | ||
} | ||
|
||
/** @return list<string> */ | ||
public function __sleep(): array | ||
{ | ||
$serialized = ['type', 'fieldName', 'name']; | ||
|
||
foreach (['length', 'columnDefinition', 'enumType', 'options'] as $stringOrArrayKey) { | ||
if ($this->$stringOrArrayKey !== null) { | ||
$serialized[] = $stringOrArrayKey; | ||
} | ||
} | ||
|
||
return $serialized; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/Doctrine/Tests/ORM/Mapping/DiscriminatorColumnMappingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Mapping; | ||
|
||
use Doctrine\ORM\Mapping\DiscriminatorColumnMapping; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function assert; | ||
use function serialize; | ||
use function unserialize; | ||
|
||
final class DiscriminatorColumnMappingTest extends TestCase | ||
{ | ||
public function testItSurvivesSerialization(): void | ||
{ | ||
$mapping = new DiscriminatorColumnMapping( | ||
type: 'string', | ||
fieldName: 'discr', | ||
name: 'discr', | ||
); | ||
|
||
$mapping->length = 255; | ||
$mapping->columnDefinition = 'VARCHAR(255)'; | ||
$mapping->enumType = 'MyEnum'; | ||
$mapping->options = ['foo' => 'bar']; | ||
|
||
$resurrectedMapping = unserialize(serialize($mapping)); | ||
assert($resurrectedMapping instanceof DiscriminatorColumnMapping); | ||
|
||
self::assertSame($resurrectedMapping->length, 255); | ||
self::assertSame($resurrectedMapping->columnDefinition, 'VARCHAR(255)'); | ||
self::assertSame($resurrectedMapping->enumType, 'MyEnum'); | ||
self::assertSame($resurrectedMapping->options, ['foo' => 'bar']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters