3 namespace Drupal\serialization\Normalizer;
5 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
8 * A trait for TimestampItem normalization functionality.
10 trait TimeStampItemNormalizerTrait {
13 * Allowed timestamps formats for the denormalizer.
15 * The denormalizer allows deserialization to timestamps from three
16 * different formats. Validation of the input data and creation of the
17 * numerical timestamp value is handled with \DateTime::createFromFormat().
18 * The list is chosen to be unambiguous and language neutral, but also common
19 * for data interchange.
23 * @see http://php.net/manual/datetime.createfromformat.php
25 protected $allowedFormats = [
26 'UNIX timestamp' => 'U',
27 'ISO 8601' => \DateTime::ISO8601,
28 'RFC 3339' => \DateTime::RFC3339,
32 * Processes normalized timestamp values to add a formatted date and format.
34 * @param array $normalized
35 * The normalized field data to process.
39 protected function processNormalizedValues(array $normalized) {
40 // Use a RFC 3339 timestamp with the time zone set to UTC to replace the
42 $date = new \DateTime();
43 $date->setTimestamp($normalized['value']);
44 $date->setTimezone(new \DateTimeZone('UTC'));
45 $normalized['value'] = $date->format(\DateTime::RFC3339);
46 // 'format' is not a property on TimestampItem fields. This is present to
47 // assist consumers of this data.
48 $normalized['format'] = \DateTime::RFC3339;
56 protected function constructValue($data, $context) {
57 // Loop through the allowed formats and create a TimestampItem from the
58 // input data if it matches the defined pattern. Since the formats are
59 // unambiguous (i.e., they reference an absolute time with a defined time
60 // zone), only one will ever match.
61 $timezone = new \DateTimeZone('UTC');
63 // First check for a provided format.
64 if (!empty($data['format']) && in_array($data['format'], $this->allowedFormats)) {
65 $date = \DateTime::createFromFormat($data['format'], $data['value'], $timezone);
66 return ['value' => $date->getTimestamp()];
68 // Otherwise, loop through formats.
70 foreach ($this->allowedFormats as $format) {
71 if (($date = \DateTime::createFromFormat($format, $data['value'], $timezone)) !== FALSE) {
72 return ['value' => $date->getTimestamp()];
79 foreach ($this->allowedFormats as $label => $format) {
80 $format_strings[] = "\"$format\" ($label)";
83 $formats = implode(', ', $format_strings);
84 throw new UnexpectedValueException(sprintf('The specified date "%s" is not in an accepted format: %s.', $data['value'], $formats));