[DoctrineBridge][PropertyInfo] Added support for Doctrine Embeddables

This commit is contained in:
Valentin 2017-06-01 23:44:40 +03:00 committed by Kévin Dunglas
parent d12f26992b
commit 7816f3b7c7
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
4 changed files with 115 additions and 1 deletions

View File

@ -50,7 +50,17 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
return;
}
return array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
$properties = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && $metadata->embeddedClasses) {
$properties = array_filter($properties, function ($property) {
return false === strpos($property, '.');
});
$properties = array_merge($properties, array_keys($metadata->embeddedClasses));
}
return $properties;
}
/**
@ -105,6 +115,10 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE
));
}
if ($metadata instanceof ClassMetadataInfo && class_exists('Doctrine\ORM\Mapping\Embedded') && isset($metadata->embeddedClasses[$property])) {
return array(new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class']));
}
if ($metadata->hasField($property)) {
$typeOfField = $metadata->getTypeOfField($property);
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);

View File

@ -62,6 +62,21 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_TestCase
);
}
public function testGetPropertiesWithEmbedded()
{
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
}
$this->assertEquals(
array(
'id',
'embedded',
),
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded')
);
}
/**
* @dataProvider typesProvider
*/
@ -70,6 +85,27 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array()));
}
public function testExtractWithEmbedded()
{
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
}
$expectedTypes = array(new Type(
Type::BUILTIN_TYPE_OBJECT,
false,
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable'
));
$actualTypes = $this->extractor->getTypes(
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded',
'embedded',
array()
);
$this->assertEquals($expectedTypes, $actualTypes);
}
public function typesProvider()
{
return array(

View File

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embeddable;
/**
* @Embeddable
*
* @author Udaltsov Valentin <udaltsov.valentin@gmail.com>
*/
class DoctrineEmbeddable
{
/**
* @Column(type="string")
*/
protected $field;
}

View File

@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Embedded;
/**
* @Entity
*
* @author Udaltsov Valentin <udaltsov.valentin@gmail.com>
*/
class DoctrineWithEmbedded
{
/**
* @Id
* @Column(type="smallint")
*/
public $id;
/**
* @Embedded(class="DoctrineEmbeddable")
*/
protected $embedded;
}