DateField::getDocumentFieldValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 1
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|null
14
     */
15
    public function getDocumentFieldValue($entity)
16
    {
17
        $result = null;
18
        $entityValue = $this->getEntityFieldValue($entity);
19
        if (!is_null($entityValue)) {
20
            if (!$entityValue instanceof \DateTime) {
21
                throw new InvalidFieldValueException(
22
                    sprintf(
23
                        '"%s" field value of "%s" must be a DateTime instance',
24
                        $this->getEntityFieldName(),
25
                        get_class($entity)
26
                    )
27
                );
28
            }
29
            $result = $entityValue->format(self::FORMAT);
30
        }
31
32
        return $result;
33
    }
34
}