Skip to content

Commit

Permalink
use getter to handle other data-types than objects, fixes #171
Browse files Browse the repository at this point in the history
  • Loading branch information
floriansemm committed Jan 22, 2018
1 parent 2312aac commit 6e64915
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
11 changes: 7 additions & 4 deletions Doctrine/Mapper/Factory/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,16 @@ public function createDocument(MetaInformationInterface $metaInformation)
continue;
}

$value = $field->getValue();
if ($value instanceof Collection) {
$fieldValue = $field->getValue();
if ($fieldValue instanceof Collection) {
$document->addField($field->getNameWithAlias(), $this->mapCollection($field, $metaInformation->getClassName()), $field->getBoost());
} elseif (is_object($value)) {
} elseif (is_object($fieldValue)) {
$document->addField($field->getNameWithAlias(), $this->mapObject($field), $field->getBoost());
} else if ($field->getter && $fieldValue) {
$getterValue = $this->callGetterMethod($metaInformation->getEntity(), $field->getGetterName());
$document->addField($field->getNameWithAlias(), $getterValue, $field->getBoost());
} else {
$document->addField($field->getNameWithAlias(), $field->getValue(), $field->getBoost());
$document->addField($field->getNameWithAlias(), $fieldValue, $field->getBoost());
}

if ($field->getFieldModifier()) {
Expand Down
4 changes: 2 additions & 2 deletions Tests/Doctrine/Annotation/AnnotationReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function testGetFields_ThreeFieldsDetected()
{
$fields = $this->reader->getFields(new ValidTestEntity());

$this->assertEquals(4, count($fields), '4 fields are mapped');
$this->assertEquals(5, count($fields), '5 fields are mapped');
}

public function testGetFields_OneFieldsOneTypes()
Expand Down Expand Up @@ -81,7 +81,7 @@ public function testGetFieldMapping_ThreeMappingsAndId()
{
$fields = $this->reader->getFieldMapping(new ValidTestEntity());

$this->assertEquals(5, count($fields), 'five fields are mapped');
$this->assertEquals(6, count($fields), 'six fields are mapped');
$this->assertTrue(array_key_exists('title', $fields));
$this->assertTrue(array_key_exists('id', $fields));
}
Expand Down
18 changes: 10 additions & 8 deletions Tests/Doctrine/Mapper/EntityMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,25 @@ public function mapRelationField_Getter()
*/
public function callGetterWithParameter()
{
$entity1 = new ValidTestEntity();
$date = new \DateTime();
$data = ['key' => 'value'];

$metaInformation = MetaTestInformationFactory::getMetaInformation($entity1);
$metaInformation->setFields(array(
new Field(array('name' => 'created_at', 'type' => 'datetime', 'boost' => '1', 'value' => $date, 'getter' => "format('d.m.Y')"))
));
$date = new \DateTime();
$entity1 = new ValidTestEntity();
$entity1->setId(uniqid());
$entity1->setCreatedAt($date);
$entity1->setComplexDataType(json_encode($data));

$fields = $metaInformation->getFields();
$metaInformation->setFields($fields);
$metaInformation = $this->metaInformationFactory->loadInformation($entity1);

$document = $this->mapper->toDocument($metaInformation);

$fields = $document->getFields();

$this->assertArrayHasKey('created_at_dt', $fields);
$this->assertEquals($date->format('d.m.Y'), $fields['created_at_dt']);
$this->assertArrayHasKey('complex_data_type', $fields);

$this->assertEquals($data, $fields['complex_data_type']);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Tests/Doctrine/Mapper/MetaInformationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function testLoadInformation_ShouldLoadAll()
$this->assertTrue($actual instanceof MetaInformation);
$this->assertEquals($expectedClassName, $actual->getClassName(), 'wrong classname');
$this->assertEquals($expectedDocumentName, $actual->getDocumentName(), 'wrong documentname');
$this->assertEquals(4, count($actual->getFields()), '4 fields are set');
$this->assertEquals(5, count($actual->getFieldMapping()), '5 fields are mapped');
$this->assertEquals(5, count($actual->getFields()), '5 fields are set');
$this->assertEquals(6, count($actual->getFieldMapping()), '5 fields are mapped');
}

public function testLoadInformation_LoadInformationFromObject()
Expand All @@ -86,7 +86,7 @@ public function testLoadInformation_LoadInformationFromObject()
$this->assertTrue($actual instanceof MetaInformation);
$this->assertEquals($expectedClassName, $actual->getClassName(), 'wrong classname');
$this->assertEquals($expectedDocumentName, $actual->getDocumentName(), 'wrong documentname');
$this->assertEquals(4, count($actual->getFields()), '4 fields are mapped');
$this->assertEquals(5, count($actual->getFields()), '5 fields are mapped');

$this->assertTrue($actual->hasField('title'), 'field should be able to located by field-name');
$this->assertTrue($actual->hasField('text_t'), 'field should be able to located by field-name with suffix');
Expand Down
28 changes: 28 additions & 0 deletions Tests/Fixtures/ValidTestEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class ValidTestEntity
*/
private $privateField;

/**
* @var array
*
* @Solr\Field(name="complex_data_type", getter="getComplexData")
*/
private $complexDataType;

/**
* @return int
*/
Expand Down Expand Up @@ -187,5 +194,26 @@ public function setPublishDate($publishDate)
{
$this->publishDate = $publishDate;
}

/**
* @return array
*/
public function getComplexDataType()
{
return $this->complexDataType;
}

/**
* @param string $complexDataType
*/
public function setComplexDataType($complexDataType)
{
$this->complexDataType = $complexDataType;
}

public function getComplexData()
{
return json_decode($this->complexDataType, true);
}
}

2 changes: 1 addition & 1 deletion Tests/SolrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testCreateQuery_ValidEntity()
$query = $this->solr->createQuery(ValidTestEntity::class);

$this->assertTrue($query instanceof SolrQuery);
$this->assertEquals(5, count($query->getMappedFields()));
$this->assertEquals(6, count($query->getMappedFields()));

}

Expand Down

0 comments on commit 6e64915

Please sign in to comment.