DateTimeEnhancer.php in JSON:API Extras 8.3
File
src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
View source
<?php
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;
use Shaper\Util\Context;
class DateTimeEnhancer extends DateTimeEnhancerBase {
protected function doUndoTransform($data, Context $context) {
$date = new \DateTime();
$date
->setTimestamp(is_int($data) ? $data : (new DrupalDateTime($data))
->getTimestamp());
$configuration = $this
->getConfiguration();
return $date
->format($configuration['dateTimeFormat']);
}
protected function doTransform($data, Context $context) {
$date = new \DateTime($data);
return (int) $date
->format('U');
}
}