Skip to content

Commit 897bd1c

Browse files
committed
date field, fixed array field
1 parent de9f3ae commit 897bd1c

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

DependencyInjection/Configuration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getConfigTreeBuilder()
6565
->scalarNode('document_field_name')->cannotBeEmpty()->end()
6666
->enumNode('field_type')
6767
->defaultValue('string')
68-
->values(['string', 'array', 'boolean', 'int', 'double'])
68+
->values(['string', 'array', 'boolean', 'int', 'double', 'date'])
6969
->end()
7070
->booleanNode('entity_primary_key')->defaultValue(false)->end()
7171
->scalarNode('priority')->defaultValue(0)->end()

Schema/Field/Entity/ArrayField.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ public function getDocumentFieldValue($entity)
1515
$entityValue = $this->getEntityFieldValue($entity);
1616
if (is_scalar($entityValue)) {
1717
$entityValue = [ $entityValue ];
18-
} elseif ((!$entityValue instanceof \Iterator) && (!$entityValue instanceof \IteratorAggregate)) {
18+
} elseif (
19+
!is_array($entityValue) &&
20+
((!$entityValue instanceof \Iterator) && (!$entityValue instanceof \IteratorAggregate))
21+
) {
1922
throw new InvalidFieldValueException('Field value must be \Iterator or \IteratorAggregate instance');
2023
}
2124

Schema/Field/Entity/DateField.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Mdiyakov\DoctrineSolrBundle\Schema\Field\Entity;
4+
5+
use Mdiyakov\DoctrineSolrBundle\Exception\InvalidFieldValueException;
6+
7+
class DateField extends Field
8+
{
9+
const FORMAT = 'Y-m-d\TH:i:s\Z';
10+
11+
/**
12+
* @param object $entity
13+
* @return string
14+
*/
15+
public function getDocumentFieldValue($entity)
16+
{
17+
$entityValue = $this->getEntityFieldValue($entity);
18+
19+
if (!$entityValue instanceof \DateTime) {
20+
throw new InvalidFieldValueException(
21+
sprintf(
22+
'"%s" field value of "%s" must be a DateTime instance',
23+
$this->getEntityFieldName(),
24+
get_class($entity)
25+
)
26+
);
27+
}
28+
29+
return $entityValue->format(self::FORMAT);
30+
}
31+
}

Schema/Field/Entity/FieldFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class FieldFactory
1010
'double' => DoubleField::class,
1111
'array' => ArrayField::class,
1212
'boolean' => BooleanField::class,
13+
'date' => DateField::class,
1314
];
1415

1516
/**

0 commit comments

Comments
 (0)