Update Composer, update everything
This commit is contained in:
parent
ea3e94409f
commit
dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions
|
|
@ -10,6 +10,10 @@ use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder;
|
|||
|
||||
/**
|
||||
* Adds 'ajax to the supported content types of the JSON encoder'
|
||||
*
|
||||
* @internal
|
||||
* This encoder should not be used directly. Rather, use the `serializer`
|
||||
* service.
|
||||
*/
|
||||
class JsonEncoder extends BaseJsonEncoder implements EncoderInterface, DecoderInterface {
|
||||
|
||||
|
|
|
|||
|
|
@ -4,16 +4,23 @@ namespace Drupal\serialization\Encoder;
|
|||
|
||||
use Symfony\Component\Serializer\Encoder\EncoderInterface;
|
||||
use Symfony\Component\Serializer\Encoder\DecoderInterface;
|
||||
use Symfony\Component\Serializer\Encoder\SerializerAwareEncoder;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder as BaseXmlEncoder;
|
||||
use Symfony\Component\Serializer\SerializerAwareInterface;
|
||||
use Symfony\Component\Serializer\SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* Adds XML support for serializer.
|
||||
*
|
||||
* This acts as a wrapper class for Symfony's XmlEncoder so that it is not
|
||||
* implementing NormalizationAwareInterface, and can be normalized externally.
|
||||
*
|
||||
* @internal
|
||||
* This encoder should not be used directly. Rather, use the `serializer`
|
||||
* service.
|
||||
*/
|
||||
class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface {
|
||||
class XmlEncoder implements SerializerAwareInterface, EncoderInterface, DecoderInterface {
|
||||
|
||||
use SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* The formats that this Encoder supports.
|
||||
|
|
@ -56,7 +63,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function encode($data, $format, array $context = []){
|
||||
public function encode($data, $format, array $context = []) {
|
||||
return $this->getBaseEncoder()->encode($data, $format, $context);
|
||||
}
|
||||
|
||||
|
|
@ -70,7 +77,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function decode($data, $format, array $context = []){
|
||||
public function decode($data, $format, array $context = []) {
|
||||
return $this->getBaseEncoder()->decode($data, $format, $context);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Drupal\serialization\EntityResolver;
|
||||
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
|
|
@ -11,20 +11,20 @@ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
|||
class UuidResolver implements EntityResolverInterface {
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
* The entity repository.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||
* @var \Drupal\Core\Entity\EntityRepositoryInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
protected $entityRepository;
|
||||
|
||||
/**
|
||||
* Constructs a UuidResolver object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
||||
* The entity repository.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager) {
|
||||
$this->entityManager = $entity_manager;
|
||||
public function __construct(EntityRepositoryInterface $entity_repository) {
|
||||
$this->entityRepository = $entity_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -35,7 +35,7 @@ class UuidResolver implements EntityResolverInterface {
|
|||
// deserialized. If it can return a UUID from that data, and if there's an
|
||||
// entity with that UUID, then return its ID.
|
||||
if (($normalizer instanceof UuidReferenceInterface) && ($uuid = $normalizer->getUuid($data))) {
|
||||
if ($entity = $this->entityManager->loadEntityByUuid($entity_type, $uuid)) {
|
||||
if ($entity = $this->entityRepository->loadEntityByUuid($entity_type, $uuid)) {
|
||||
return $entity->id();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ class BcConfigSubscriber implements EventSubscriberInterface {
|
|||
$saved_config = $event->getConfig();
|
||||
|
||||
if ($saved_config->getName() === 'serialization.settings') {
|
||||
if ($event->isChanged('bc_primitives_as_strings')) {
|
||||
if ($event->isChanged('bc_primitives_as_strings') || $event->isChanged('bc_timestamp_normalizer_unix')) {
|
||||
$this->kernel->invalidateContainer();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\serialization\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Cache\CacheableDependencyInterface;
|
||||
use Drupal\Core\Cache\CacheableResponse;
|
||||
use Drupal\Core\EventSubscriber\HttpExceptionSubscriberBase;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
||||
|
|
@ -51,8 +53,12 @@ class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {
|
|||
*/
|
||||
protected static function getPriority() {
|
||||
// This will fire after the most common HTML handler, since HTML requests
|
||||
// are still more common than HTTP requests.
|
||||
return -75;
|
||||
// are still more common than HTTP requests. But it has a lower priority
|
||||
// than \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber::on4xx(), so
|
||||
// that this also handles the 'json' format. Then all serialization formats
|
||||
// (::getHandledFormats()) are handled by this exception subscriber, which
|
||||
// results in better consistency.
|
||||
return -70;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -67,14 +73,22 @@ class DefaultExceptionSubscriber extends HttpExceptionSubscriberBase {
|
|||
$request = $event->getRequest();
|
||||
|
||||
$format = $request->getRequestFormat();
|
||||
$content = ['message' => $event->getException()->getMessage()];
|
||||
$content = ['message' => $exception->getMessage()];
|
||||
$encoded_content = $this->serializer->serialize($content, $format);
|
||||
$headers = $exception->getHeaders();
|
||||
|
||||
// Add the MIME type from the request to send back in the header.
|
||||
$headers['Content-Type'] = $request->getMimeType($format);
|
||||
|
||||
$response = new Response($encoded_content, $exception->getStatusCode(), $headers);
|
||||
// If the exception is cacheable, generate a cacheable response.
|
||||
if ($exception instanceof CacheableDependencyInterface) {
|
||||
$response = new CacheableResponse($encoded_content, $exception->getStatusCode(), $headers);
|
||||
$response->addCacheableDependency($exception);
|
||||
}
|
||||
else {
|
||||
$response = new Response($encoded_content, $exception->getStatusCode(), $headers);
|
||||
}
|
||||
|
||||
$event->setResponse($response);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ class UserRouteAlterSubscriber implements EventSubscriberInterface {
|
|||
'user.login_status.http',
|
||||
'user.login.http',
|
||||
'user.logout.http',
|
||||
'user.pass.http',
|
||||
];
|
||||
$routes = $event->getRouteCollection();
|
||||
foreach ($route_names as $route_name) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
|
||||
/**
|
||||
* Defines the interface for normalizers producing cacheable normalizations.
|
||||
*
|
||||
* @see cache
|
||||
*/
|
||||
interface CacheableNormalizerInterface extends NormalizerInterface {
|
||||
|
||||
/**
|
||||
* Name of key for bubbling cacheability metadata via serialization context.
|
||||
*
|
||||
* @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
|
||||
* @see \Symfony\Component\Serializer\SerializerInterface::serialize()
|
||||
* @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::renderResponseBody()
|
||||
*/
|
||||
const SERIALIZATION_CONTEXT_CACHEABILITY = 'cacheability';
|
||||
|
||||
}
|
||||
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\TypedData\ComplexDataInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
|
||||
|
||||
/**
|
||||
* Converts the Drupal entity object structures to a normalized array.
|
||||
*
|
||||
|
|
@ -26,9 +29,19 @@ class ComplexDataNormalizer extends NormalizerBase {
|
|||
*/
|
||||
public function normalize($object, $format = NULL, array $context = []) {
|
||||
$attributes = [];
|
||||
/** @var \Drupal\Core\TypedData\TypedDataInterface $field */
|
||||
foreach ($object as $name => $field) {
|
||||
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
|
||||
// $object will not always match $supportedInterfaceOrClass.
|
||||
// @see \Drupal\serialization\Normalizer\EntityNormalizer
|
||||
// Other normalizers that extend this class may only provide $object that
|
||||
// implements \Traversable.
|
||||
if ($object instanceof ComplexDataInterface) {
|
||||
// If there are no properties to normalize, just normalize the value.
|
||||
$object = !empty($object->getProperties(TRUE))
|
||||
? TypedDataInternalPropertiesHelper::getNonInternalProperties($object)
|
||||
: $object->getValue();
|
||||
}
|
||||
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
|
||||
foreach ($object as $name => $property) {
|
||||
$attributes[$name] = $this->serializer->normalize($property, $format, $context);
|
||||
}
|
||||
return $attributes;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,33 @@ class ConfigEntityNormalizer extends EntityNormalizer {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($object, $format = NULL, array $context = []) {
|
||||
return $object->toArray();
|
||||
return static::getDataWithoutInternals($object->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = NULL, array $context = []) {
|
||||
return parent::denormalize(static::getDataWithoutInternals($data), $class, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the given data without the internal implementation details.
|
||||
*
|
||||
* @param array $data
|
||||
* The data that is either currently or about to be stored in configuration.
|
||||
*
|
||||
* @return array
|
||||
* The same data, but without internals. Currently, that is only the '_core'
|
||||
* key, which is reserved by Drupal core to handle complex edge cases
|
||||
* correctly. Data in the '_core' key is irrelevant to clients reading
|
||||
* configuration, and is not allowed to be set by clients writing
|
||||
* configuration: it is for Drupal core only, and managed by Drupal core.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2653358
|
||||
*/
|
||||
protected static function getDataWithoutInternals(array $data) {
|
||||
return array_diff_key($data, ['_core' => TRUE]);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
|
||||
|
||||
/**
|
||||
* Normalizes/denormalizes Drupal content entities into an array structure.
|
||||
*/
|
||||
|
|
@ -15,15 +17,16 @@ class ContentEntityNormalizer extends EntityNormalizer {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($object, $format = NULL, array $context = []) {
|
||||
public function normalize($entity, $format = NULL, array $context = []) {
|
||||
$context += [
|
||||
'account' => NULL,
|
||||
];
|
||||
|
||||
$attributes = [];
|
||||
foreach ($object as $name => $field) {
|
||||
if ($field->access('view', $context['account'])) {
|
||||
$attributes[$name] = $this->serializer->normalize($field, $format, $context);
|
||||
/** @var \Drupal\Core\Entity\Entity $entity */
|
||||
foreach (TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData()) as $name => $field_items) {
|
||||
if ($field_items->access('view', $context['account'])) {
|
||||
$attributes[$name] = $this->serializer->normalize($field_items, $format, $context);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,13 +40,20 @@ class EntityNormalizer extends ComplexDataNormalizer implements DenormalizerInte
|
|||
|
||||
// The bundle property will be required to denormalize a bundleable
|
||||
// fieldable entity.
|
||||
if ($entity_type_definition->hasKey('bundle') && $entity_type_definition->isSubclassOf(FieldableEntityInterface::class)) {
|
||||
// Get an array containing the bundle only. This also remove the bundle
|
||||
// key from the $data array.
|
||||
$bundle_data = $this->extractBundleData($data, $entity_type_definition);
|
||||
if ($entity_type_definition->entityClassImplements(FieldableEntityInterface::class)) {
|
||||
// Extract bundle data to pass into entity creation if the entity type uses
|
||||
// bundles.
|
||||
if ($entity_type_definition->hasKey('bundle')) {
|
||||
// Get an array containing the bundle only. This also remove the bundle
|
||||
// key from the $data array.
|
||||
$create_params = $this->extractBundleData($data, $entity_type_definition);
|
||||
}
|
||||
else {
|
||||
$create_params = [];
|
||||
}
|
||||
|
||||
// Create the entity from bundle data only, then apply field values after.
|
||||
$entity = $this->entityManager->getStorage($entity_type_id)->create($bundle_data);
|
||||
$entity = $this->entityManager->getStorage($entity_type_id)->create($create_params);
|
||||
|
||||
$this->denormalizeFieldData($data, $entity, $format, $context);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,12 +2,17 @@
|
|||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\Entity\EntityRepositoryInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Adds the file URI to embedded file entities.
|
||||
*/
|
||||
class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
|
||||
class EntityReferenceFieldItemNormalizer extends FieldItemNormalizer {
|
||||
|
||||
use EntityReferenceFieldItemNormalizerTrait;
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
|
|
@ -16,12 +21,31 @@ class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
|
|||
*/
|
||||
protected $supportedInterfaceOrClass = EntityReferenceItem::class;
|
||||
|
||||
/**
|
||||
* The entity repository.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityRepositoryInterface
|
||||
*/
|
||||
protected $entityRepository;
|
||||
|
||||
/**
|
||||
* Constructs a EntityReferenceFieldItemNormalizer object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
||||
* The entity repository.
|
||||
*/
|
||||
public function __construct(EntityRepositoryInterface $entity_repository) {
|
||||
$this->entityRepository = $entity_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = []) {
|
||||
$values = parent::normalize($field_item, $format, $context);
|
||||
|
||||
$this->normalizeRootReferenceValue($values, $field_item);
|
||||
|
||||
/** @var \Drupal\Core\Entity\EntityInterface $entity */
|
||||
if ($entity = $field_item->get('entity')->getValue()) {
|
||||
$values['target_type'] = $entity->getEntityTypeId();
|
||||
|
|
@ -39,4 +63,29 @@ class EntityReferenceFieldItemNormalizer extends ComplexDataNormalizer {
|
|||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function constructValue($data, $context) {
|
||||
if (isset($data['target_uuid'])) {
|
||||
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item */
|
||||
$field_item = $context['target_instance'];
|
||||
if (empty($data['target_uuid'])) {
|
||||
throw new InvalidArgumentException(sprintf('If provided "target_uuid" cannot be empty for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
|
||||
}
|
||||
$target_type = $field_item->getFieldDefinition()->getSetting('target_type');
|
||||
if (!empty($data['target_type']) && $target_type !== $data['target_type']) {
|
||||
throw new UnexpectedValueException(sprintf('The field "%s" property "target_type" must be set to "%s" or omitted.', $field_item->getFieldDefinition()->getName(), $target_type));
|
||||
}
|
||||
if ($entity = $this->entityRepository->loadEntityByUuid($target_type, $data['target_uuid'])) {
|
||||
return ['target_id' => $entity->id()] + array_intersect_key($data, $field_item->getProperties());
|
||||
}
|
||||
else {
|
||||
// Unable to load entity by uuid.
|
||||
throw new InvalidArgumentException(sprintf('No "%s" entity found with UUID "%s" for field "%s".', $data['target_type'], $data['target_uuid'], $field_item->getName()));
|
||||
}
|
||||
}
|
||||
return parent::constructValue($data, $context);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
|
||||
|
||||
/**
|
||||
* Converts empty reference values for entity reference items.
|
||||
*/
|
||||
trait EntityReferenceFieldItemNormalizerTrait {
|
||||
|
||||
protected function normalizeRootReferenceValue(&$values, EntityReferenceItem $field_item) {
|
||||
// @todo Generalize for all tree-structured entity types.
|
||||
if ($this->fieldItemReferencesTaxonomyTerm($field_item) && empty($values['target_id'])) {
|
||||
$values['target_id'] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a field item references a taxonomy term.
|
||||
*
|
||||
* @param \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $field_item
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function fieldItemReferencesTaxonomyTerm(EntityReferenceItem $field_item) {
|
||||
return $field_item->getFieldDefinition()->getSetting('target_type') === 'taxonomy_term';
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -35,7 +35,7 @@ class FieldNormalizer extends ListNormalizer implements DenormalizerInterface {
|
|||
throw new InvalidArgumentException('The field passed in via $context[\'target_instance\'] must have a parent set.');
|
||||
}
|
||||
|
||||
/** @var FieldItemListInterface $items */
|
||||
/** @var \Drupal\Core\Field\FieldItemListInterface $items */
|
||||
$items = $context['target_instance'];
|
||||
$item_class = $items->getItemDefinition()->getClass();
|
||||
|
||||
|
|
|
|||
|
|
@ -2,13 +2,16 @@
|
|||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||||
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
|
||||
use Drupal\Core\Cache\CacheableDependencyInterface;
|
||||
use Symfony\Component\Serializer\SerializerAwareInterface;
|
||||
use Symfony\Component\Serializer\SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* Base class for Normalizers.
|
||||
*/
|
||||
abstract class NormalizerBase extends SerializerAwareNormalizer implements NormalizerInterface {
|
||||
abstract class NormalizerBase implements SerializerAwareInterface, CacheableNormalizerInterface {
|
||||
|
||||
use SerializerAwareTrait;
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
|
|
@ -17,6 +20,13 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
|
|||
*/
|
||||
protected $supportedInterfaceOrClass;
|
||||
|
||||
/**
|
||||
* List of formats which supports (de-)normalization.
|
||||
*
|
||||
* @var string|string[]
|
||||
*/
|
||||
protected $format;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
@ -29,7 +39,7 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
|
|||
|
||||
$supported = (array) $this->supportedInterfaceOrClass;
|
||||
|
||||
return (bool) array_filter($supported, function($name) use ($data) {
|
||||
return (bool) array_filter($supported, function ($name) use ($data) {
|
||||
return $data instanceof $name;
|
||||
});
|
||||
}
|
||||
|
|
@ -49,7 +59,7 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
|
|||
|
||||
$supported = (array) $this->supportedInterfaceOrClass;
|
||||
|
||||
$subclass_check = function($name) use ($type) {
|
||||
$subclass_check = function ($name) use ($type) {
|
||||
return (class_exists($name) || interface_exists($name)) && is_subclass_of($type, $name, TRUE);
|
||||
};
|
||||
|
||||
|
|
@ -71,7 +81,21 @@ abstract class NormalizerBase extends SerializerAwareNormalizer implements Norma
|
|||
return TRUE;
|
||||
}
|
||||
|
||||
return in_array($format, (array) $this->format);
|
||||
return in_array($format, (array) $this->format, TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds cacheability if applicable.
|
||||
*
|
||||
* @param array $context
|
||||
* Context options for the normalizer.
|
||||
* @param $data
|
||||
* The data that might have cacheability information.
|
||||
*/
|
||||
protected function addCacheableDependency(array $context, $data) {
|
||||
if ($data instanceof CacheableDependencyInterface && isset($context[static::SERIALIZATION_CONTEXT_CACHEABILITY])) {
|
||||
$context[static::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($data);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* A trait for TimestampItem normalization functionality.
|
||||
*/
|
||||
trait TimeStampItemNormalizerTrait {
|
||||
|
||||
/**
|
||||
* Allowed timestamps formats for the denormalizer.
|
||||
*
|
||||
* The denormalizer allows deserialization to timestamps from three
|
||||
* different formats. Validation of the input data and creation of the
|
||||
* numerical timestamp value is handled with \DateTime::createFromFormat().
|
||||
* The list is chosen to be unambiguous and language neutral, but also common
|
||||
* for data interchange.
|
||||
*
|
||||
* @var string[]
|
||||
*
|
||||
* @see http://php.net/manual/datetime.createfromformat.php
|
||||
*/
|
||||
protected $allowedFormats = [
|
||||
'UNIX timestamp' => 'U',
|
||||
'ISO 8601' => \DateTime::ISO8601,
|
||||
'RFC 3339' => \DateTime::RFC3339,
|
||||
];
|
||||
|
||||
/**
|
||||
* Processes normalized timestamp values to add a formatted date and format.
|
||||
*
|
||||
* @param array $normalized
|
||||
* The normalized field data to process.
|
||||
* @return array
|
||||
* The processed data.
|
||||
*/
|
||||
protected function processNormalizedValues(array $normalized) {
|
||||
// Use a RFC 3339 timestamp with the time zone set to UTC to replace the
|
||||
// timestamp value.
|
||||
$date = new \DateTime();
|
||||
$date->setTimestamp($normalized['value']);
|
||||
$date->setTimezone(new \DateTimeZone('UTC'));
|
||||
$normalized['value'] = $date->format(\DateTime::RFC3339);
|
||||
// 'format' is not a property on TimestampItem fields. This is present to
|
||||
// assist consumers of this data.
|
||||
$normalized['format'] = \DateTime::RFC3339;
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function constructValue($data, $context) {
|
||||
// Loop through the allowed formats and create a TimestampItem from the
|
||||
// input data if it matches the defined pattern. Since the formats are
|
||||
// unambiguous (i.e., they reference an absolute time with a defined time
|
||||
// zone), only one will ever match.
|
||||
$timezone = new \DateTimeZone('UTC');
|
||||
|
||||
// First check for a provided format.
|
||||
if (!empty($data['format']) && in_array($data['format'], $this->allowedFormats)) {
|
||||
$date = \DateTime::createFromFormat($data['format'], $data['value'], $timezone);
|
||||
return ['value' => $date->getTimestamp()];
|
||||
}
|
||||
// Otherwise, loop through formats.
|
||||
else {
|
||||
foreach ($this->allowedFormats as $format) {
|
||||
if (($date = \DateTime::createFromFormat($format, $data['value'], $timezone)) !== FALSE) {
|
||||
return ['value' => $date->getTimestamp()];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$format_strings = [];
|
||||
|
||||
foreach ($this->allowedFormats as $label => $format) {
|
||||
$format_strings[] = "\"$format\" ($label)";
|
||||
}
|
||||
|
||||
$formats = implode(', ', $format_strings);
|
||||
throw new UnexpectedValueException(sprintf('The specified date "%s" is not in an accepted format: %s.', $data['value'], $formats));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\serialization\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Converts values for TimestampItem to and from common formats.
|
||||
*/
|
||||
class TimestampItemNormalizer extends FieldItemNormalizer {
|
||||
|
||||
use TimeStampItemNormalizerTrait;
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = TimestampItem::class;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = []) {
|
||||
$data = parent::normalize($field_item, $format, $context);
|
||||
|
||||
return $this->processNormalizedValues($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = NULL, array $context = []) {
|
||||
if (empty($data['value'])) {
|
||||
throw new InvalidArgumentException('No "value" attribute present');
|
||||
}
|
||||
|
||||
return parent::denormalize($data, $class, $format, $context);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -18,7 +18,13 @@ class TypedDataNormalizer extends NormalizerBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($object, $format = NULL, array $context = []) {
|
||||
return $object->getValue();
|
||||
$this->addCacheableDependency($context, $object);
|
||||
$value = $object->getValue();
|
||||
// Support for stringable value objects: avoid numerous custom normalizers.
|
||||
if (is_object($value) && method_exists($value, '__toString')) {
|
||||
$value = (string) $value;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ class RegisterSerializationClassesCompilerPass implements CompilerPassInterface
|
|||
|
||||
// Retrieve registered Normalizers and Encoders from the container.
|
||||
foreach ($container->findTaggedServiceIds('normalizer') as $id => $attributes) {
|
||||
// The 'serializer' service is the public API: mark normalizers private.
|
||||
$container->getDefinition($id)->setPublic(FALSE);
|
||||
|
||||
// If there is a BC key present, pass this to determine if the normalizer
|
||||
// should be skipped.
|
||||
if (isset($attributes[0]['bc']) && $this->normalizerBcSettingIsEnabled($attributes[0]['bc'], $attributes[0]['bc_config_name'])) {
|
||||
|
|
@ -33,6 +36,9 @@ class RegisterSerializationClassesCompilerPass implements CompilerPassInterface
|
|||
$normalizers[$priority][] = new Reference($id);
|
||||
}
|
||||
foreach ($container->findTaggedServiceIds('encoder') as $id => $attributes) {
|
||||
// The 'serializer' service is the public API: mark encoders private.
|
||||
$container->getDefinition($id)->setPublic(FALSE);
|
||||
|
||||
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
|
||||
$encoders[$priority][] = new Reference($id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,4 +10,4 @@ use Drupal\Tests\serialization\Kernel\NormalizerTestBase as SerializationNormali
|
|||
* @deprecated Scheduled for removal in Drupal 9.0.0.
|
||||
* Use \Drupal\Tests\serialization\Kernel\NormalizerTestBase instead.
|
||||
*/
|
||||
abstract class NormalizerTestBase extends SerializationNormalizerTestBase { }
|
||||
abstract class NormalizerTestBase extends SerializationNormalizerTestBase {}
|
||||
|
|
|
|||
Reference in a new issue