-
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.
Merge pull request #10612 from doctrine/embedded-class-mapping-dto
- Loading branch information
Showing
10 changed files
with
179 additions
and
66 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
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,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Mapping; | ||
|
||
use ArrayAccess; | ||
|
||
use function property_exists; | ||
|
||
/** @template-implements ArrayAccess<string, mixed> */ | ||
final class EmbeddedClassMapping implements ArrayAccess | ||
{ | ||
use ArrayAccessImplementation; | ||
|
||
public string|false|null $columnPrefix = null; | ||
public string|null $declaredField = null; | ||
public string|null $originalField = null; | ||
|
||
/** | ||
* This is set when this embedded-class field is inherited by this class | ||
* from another (inheritance) parent <em>entity</em> class. The value is | ||
* the FQCN of the topmost entity class that contains mapping information | ||
* for this field. (If there are transient classes in the class hierarchy, | ||
* these are ignored, so the class property may in fact come from a class | ||
* further up in the PHP class hierarchy.) Fields initially declared in | ||
* mapped superclasses are <em>not</em> considered 'inherited' in the | ||
* nearest entity subclasses. | ||
* | ||
* @var class-string|null | ||
*/ | ||
public string|null $inherited = null; | ||
|
||
/** | ||
* This is set when the embedded-class field does not appear for the first | ||
* time in this class, but is originally declared in another parent | ||
* <em>entity or mapped superclass</em>. The value is the FQCN of the | ||
* topmost non-transient class that contains mapping information for this | ||
* field. | ||
* | ||
* @var class-string|null | ||
*/ | ||
public string|null $declared = null; | ||
|
||
/** @param class-string $class */ | ||
public function __construct(public string $class) | ||
{ | ||
} | ||
|
||
/** | ||
* @psalm-param array{ | ||
* class: class-string, | ||
* columnPrefix?: false|string|null, | ||
* declaredField?: string|null, | ||
* originalField?: string|null | ||
* } $mappingArray | ||
*/ | ||
public static function fromMappingArray(array $mappingArray): self | ||
{ | ||
$mapping = new self($mappingArray['class']); | ||
foreach ($mappingArray as $key => $value) { | ||
if ($key === 'class') { | ||
continue; | ||
} | ||
|
||
if (property_exists($mapping, $key)) { | ||
$mapping->$key = $value; | ||
} | ||
} | ||
|
||
return $mapping; | ||
} | ||
|
||
/** @return list<string> */ | ||
public function __sleep(): array | ||
{ | ||
$serialized = ['class']; | ||
|
||
if ($this->columnPrefix) { | ||
$serialized[] = 'columnPrefix'; | ||
} | ||
|
||
foreach (['declaredField', 'originalField', 'inherited', 'declared'] as $property) { | ||
if ($this->$property !== null) { | ||
$serialized[] = $property; | ||
} | ||
} | ||
|
||
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
34 changes: 34 additions & 0 deletions
34
tests/Doctrine/Tests/ORM/Mapping/EmbeddedClassMappingTest.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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Mapping; | ||
|
||
use Doctrine\ORM\Mapping\EmbeddedClassMapping; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
use function assert; | ||
use function serialize; | ||
use function unserialize; | ||
|
||
final class EmbeddedClassMappingTest extends TestCase | ||
{ | ||
public function testItSurvivesSerialization(): void | ||
{ | ||
$mapping = new EmbeddedClassMapping(self::class); | ||
$mapping->columnPrefix = 'these'; | ||
$mapping->declaredField = 'values'; | ||
$mapping->originalField = 'make'; | ||
$mapping->inherited = self::class; // no | ||
$mapping->declared = self::class; // sense | ||
|
||
$resurrectedMapping = unserialize(serialize($mapping)); | ||
assert($resurrectedMapping instanceof EmbeddedClassMapping); | ||
|
||
self::assertSame('these', $resurrectedMapping->columnPrefix); | ||
self::assertSame('values', $resurrectedMapping->declaredField); | ||
self::assertSame('make', $resurrectedMapping->originalField); | ||
self::assertSame(self::class, $resurrectedMapping->inherited); | ||
self::assertSame(self::class, $resurrectedMapping->declared); | ||
} | ||
} |
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
Oops, something went wrong.