a275f646f7426a519506cbc458f83b8408f772da
[yaffs-website] / web / core / modules / rest / tests / src / Functional / BcTimestampNormalizerUnixTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional;
4
5 /**
6  * Trait for ResourceTestBase subclasses formatting expected timestamp data.
7  */
8 trait BcTimestampNormalizerUnixTestTrait {
9
10   /**
11    * Formats a UNIX timestamp.
12    *
13    * Depending on the 'bc_timestamp_normalizer_unix' setting. The return will be
14    * an RFC3339 date string or the same timestamp that was passed in.
15    *
16    * @param int $timestamp
17    *   The timestamp value to format.
18    *
19    * @return string|int
20    *   The formatted RFC3339 date string or UNIX timestamp.
21    *
22    * @see \Drupal\serialization\Normalizer\TimestampItemNormalizer
23    */
24   protected function formatExpectedTimestampItemValues($timestamp) {
25     // If the setting is enabled, just return the timestamp as-is now.
26     if ($this->config('serialization.settings')->get('bc_timestamp_normalizer_unix')) {
27       return ['value' => $timestamp];
28     }
29
30     // Otherwise, format the date string to the same that
31     // \Drupal\serialization\Normalizer\TimestampItemNormalizer will produce.
32     $date = new \DateTime();
33     $date->setTimestamp($timestamp);
34     $date->setTimezone(new \DateTimeZone('UTC'));
35
36     // Format is also added to the expected return values.
37     return [
38       'value' => $date->format(\DateTime::RFC3339),
39       'format' => \DateTime::RFC3339,
40     ];
41   }
42
43 }