From 8fd28fcd8ffd7ce982f3785e01bd59875d018836 Mon Sep 17 00:00:00 2001 From: Guido Contreras Woda Date: Tue, 16 Dec 2014 22:30:28 -0300 Subject: [PATCH] Added embedded and embeddables to ClassMetadataBuilder --- .../Mapping/Builder/ClassMetadataBuilder.php | 45 ++++++++++ .../ORM/Mapping/Builder/EmbeddedBuilder.php | 74 ++++++++++++++++ .../ORM/Mapping/ClassMetadataBuilderTest.php | 85 +++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 lib/Doctrine/ORM/Mapping/Builder/EmbeddedBuilder.php diff --git a/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php index 2b0d74b7d..9b0346d4e 100644 --- a/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php +++ b/lib/Doctrine/ORM/Mapping/Builder/ClassMetadataBuilder.php @@ -62,6 +62,31 @@ class ClassMetadataBuilder public function setMappedSuperClass() { $this->cm->isMappedSuperclass = true; + $this->cm->isEmbeddedClass = false; + + return $this; + } + + /** + * Marks the class as embeddable. + * + * @return ClassMetadataBuilder + */ + public function setEmbeddable() + { + $this->cm->isEmbeddedClass = true; + $this->cm->isMappedSuperclass = false; + + return $this; + } + + public function addEmbedded($fieldName, $class, $columnPrefix = null) + { + $this->cm->mapEmbedded(array( + 'fieldName' => $fieldName, + 'class' => $class, + 'columnPrefix' => $columnPrefix + )); return $this; } @@ -298,6 +323,26 @@ class ClassMetadataBuilder ); } + /** + * Creates an embedded builder. + * + * @param string $fieldName + * @param string $class + * + * @return EmbeddedBuilder + */ + public function createEmbedded($fieldName, $class) + { + return new EmbeddedBuilder( + $this, + array( + 'fieldName' => $fieldName, + 'class' => $class, + 'columnPrefix' => null + ) + ); + } + /** * Adds a simple many to one association, optionally with the inversed by field. * diff --git a/lib/Doctrine/ORM/Mapping/Builder/EmbeddedBuilder.php b/lib/Doctrine/ORM/Mapping/Builder/EmbeddedBuilder.php new file mode 100644 index 000000000..9a285a1be --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/Builder/EmbeddedBuilder.php @@ -0,0 +1,74 @@ +. + */ + +namespace Doctrine\ORM\Mapping\Builder; + +/** + * Embedded Builder + * + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link www.doctrine-project.com + * @since 2.5 + * @author Guido Contreras Woda + */ +class EmbeddedBuilder +{ + /** + * @var ClassMetadataBuilder + */ + private $builder; + + /** + * @var array + */ + private $mapping; + + /** + * @param ClassMetadataBuilder $builder + * @param array $mapping + */ + public function __construct(ClassMetadataBuilder $builder, array $mapping) + { + $this->builder = $builder; + $this->mapping = $mapping; + } + + public function setColumnPrefix($columnPrefix) + { + $this->mapping['columnPrefix'] = $columnPrefix; + + return $this; + } + + /** + * Finalizes this embeddable and attach it to the ClassMetadata. + * + * Without this call an EmbeddedBuilder has no effect on the ClassMetadata. + * + * @return ClassMetadataBuilder + */ + public function build() + { + $cm = $this->builder->getClassMetadata(); + + $cm->mapEmbedded($this->mapping); + + return $this->builder; + } +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php index abdddb737..ad236b2d9 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataBuilderTest.php @@ -48,6 +48,91 @@ class ClassMetadataBuilderTest extends \Doctrine\Tests\OrmTestCase { $this->assertIsFluent($this->builder->setMappedSuperClass()); $this->assertTrue($this->cm->isMappedSuperclass); + $this->assertFalse($this->cm->isEmbeddedClass); + } + + public function testSetEmbedable() + { + $this->assertIsFluent($this->builder->setEmbeddable()); + $this->assertTrue($this->cm->isEmbeddedClass); + $this->assertFalse($this->cm->isMappedSuperclass); + } + + public function testAddEmbeddedWithOnlyRequiredParams() + { + $this->assertIsFluent( + $this->builder->addEmbedded( + 'name', + 'Doctrine\Tests\Models\ValueObjects\Name' + ) + ); + + $this->assertEquals(array( + 'name' => array( + 'class' => 'Doctrine\Tests\Models\ValueObjects\Name', + 'columnPrefix' => null, + 'declaredField' => null, + 'originalField' => null, + ) + ), $this->cm->embeddedClasses); + } + + public function testAddEmbeddedWithPrefix() + { + $this->assertIsFluent( + $this->builder->addEmbedded( + 'name', + 'Doctrine\Tests\Models\ValueObjects\Name', + 'nm_' + ) + ); + + $this->assertEquals(array( + 'name' => array( + 'class' => 'Doctrine\Tests\Models\ValueObjects\Name', + 'columnPrefix' => 'nm_', + 'declaredField' => null, + 'originalField' => null, + ) + ), $this->cm->embeddedClasses); + } + + public function testCreateEmbeddedWithoutExtraParams() + { + $embeddedBuilder = ($this->builder->createEmbedded('name', 'Doctrine\Tests\Models\ValueObjects\Name')); + $this->assertInstanceOf('Doctrine\ORM\Mapping\Builder\EmbeddedBuilder', $embeddedBuilder); + + $this->assertFalse(isset($this->cm->embeddedClasses['name'])); + + $this->assertIsFluent($embeddedBuilder->build()); + $this->assertEquals( + array( + 'class' => 'Doctrine\Tests\Models\ValueObjects\Name', + 'columnPrefix' => null, + 'declaredField' => null, + 'originalField' => null + ), + $this->cm->embeddedClasses['name'] + ); + } + + public function testCreateEmbeddedWithColumnPrefix() + { + $embeddedBuilder = ($this->builder->createEmbedded('name', 'Doctrine\Tests\Models\ValueObjects\Name')); + + $this->assertEquals($embeddedBuilder, $embeddedBuilder->setColumnPrefix('nm_')); + + $this->assertIsFluent($embeddedBuilder->build()); + + $this->assertEquals( + array( + 'class' => 'Doctrine\Tests\Models\ValueObjects\Name', + 'columnPrefix' => 'nm_', + 'declaredField' => null, + 'originalField' => null + ), + $this->cm->embeddedClasses['name'] + ); } public function testSetCustomRepositoryClass()