You are here

protected function DateTimeFromStringEnhancer::doUndoTransform in JSON:API Extras 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\DateTimeFromStringEnhancer::doUndoTransform()

File

src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php, line 24

Class

DateTimeFromStringEnhancer
Perform additional manipulations to datetime fields.

Namespace

Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer

Code

protected function doUndoTransform($data, Context $context) {
  $configuration = $this
    ->getConfiguration();
  $reformat = function ($input) use ($configuration) {
    $storage_timezone = new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE);
    $date = new \DateTime($input, $storage_timezone);
    $output_timezone = new \DateTimezone(date_default_timezone_get());
    $date
      ->setTimezone($output_timezone);
    $output = $date
      ->format($configuration['dateTimeFormat']);
    return $output;
  };
  $result = is_array($data) ? array_map($reformat, $data) : $reformat($data);
  return $result;
}