forked from netgen/NetgenBirthdayBundle
/
Value.php
52 lines (44 loc) · 1.11 KB
/
Value.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Netgen\Bundle\BirthdayBundle\Core\FieldType\Birthday;
use DateTime;
use eZ\Publish\Core\FieldType\Value as BaseValue;
class Value extends BaseValue
{
/**
* @var \DateTime
*/
public $date = null;
/**
* Date format to be used by {@link __toString()}.
*
* @var string
*/
protected $dateFormat = 'Y-m-d';
/**
* Construct a new Value object and initialize with $dateTime.
*
* @param \DateTime|string|null $date Date as a DateTime object or string in Y-m-d format
*/
public function __construct($date = null)
{
if ($date instanceof DateTime) {
$date = clone $date;
$date->setTime(0, 0, 0);
$this->date = $date;
} elseif (is_string($date)) {
$this->date = new DateTime($date);
}
}
/**
* Returns a string representation of the field value.
*
* @return string
*/
public function __toString()
{
if (!$this->date instanceof DateTime) {
return '';
}
return $this->date->format($this->dateFormat);
}
}