Advertisement
Guest User

core/modules/serialization/src/Normalizer/NormalizerBase.php

a guest
Feb 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\serialization\Normalizer;
  4.  
  5. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  6. use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
  7.  
  8. use Drupal\Component\Plugin\PluginInspectionInterface;
  9. use Drupal\Core\Field\FieldItemInterface;
  10.  
  11. /**
  12. * Base class for Normalizers.
  13. */
  14. abstract class NormalizerBase extends SerializerAwareNormalizer implements NormalizerInterface {
  15.  
  16. /**
  17. * The interface or class that this Normalizer supports.
  18. *
  19. * @var string|array
  20. */
  21. protected $supportedInterfaceOrClass;
  22.  
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function supportsNormalization($data, $format = NULL) {
  27. // If we aren't dealing with an object or the format is not supported return
  28. // now.
  29. if (!is_object($data) || !$this->checkFormat($format)) {
  30. return FALSE;
  31. }
  32.  
  33. $supported = (array) $this->supportedInterfaceOrClass;
  34.  
  35. return (bool) array_filter($supported, function($name) use ($data) {
  36. return $data instanceof $name;
  37. });
  38. }
  39.  
  40. /**
  41. * Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()
  42. *
  43. * This class doesn't implement DenormalizerInterface, but most of its child
  44. * classes do, so this method is implemented at this level to reduce code
  45. * duplication.
  46. */
  47. public function supportsDenormalization($data, $type, $format = NULL) {
  48. // If the format is not supported return now.
  49. if (!$this->checkFormat($format)) {
  50. return FALSE;
  51. }
  52.  
  53. $supported = (array) $this->supportedInterfaceOrClass;
  54.  
  55. $subclass_check = function($name) use ($type) {
  56. return (class_exists($name) || interface_exists($name)) && is_subclass_of($type, $name, TRUE);
  57. };
  58.  
  59. return in_array($type, $supported) || array_filter($supported, $subclass_check);
  60. }
  61.  
  62. /**
  63. * Checks if the provided format is supported by this normalizer.
  64. *
  65. * @param string $format
  66. * The format to check.
  67. *
  68. * @return bool
  69. * TRUE if the format is supported, FALSE otherwise. If no format is
  70. * specified this will return TRUE.
  71. */
  72. protected function checkFormat($format = NULL) {
  73. if (!isset($format) || !isset($this->format)) {
  74. return TRUE;
  75. }
  76.  
  77. return in_array($format, (array) $this->format);
  78. }
  79.  
  80. // trait SerializedColumnNormalizerTrait {
  81.  
  82. /**
  83. * Checks if there is a serialized string for a column.
  84. *
  85. * @param mixed $data
  86. * The field item data to denormalize.
  87. * @param string $class
  88. * The expected class to instantiate.
  89. * @param \Drupal\Core\Field\FieldItemInterface $field_item
  90. * The field item.
  91. */
  92. protected function pmCheckForSerializedStrings($data, $class, FieldItemInterface $field_item) {
  93. error_log('pmCheckForSerializedStrings');
  94. // Require specialized denormalizers for fields with 'serialize' columns.
  95. // Note: this cannot be checked in ::supportsDenormalization() because at
  96. // that time we only have the field item class. ::hasSerializeColumn()
  97. // must be able to call $field_item->schema(), which requires a field
  98. // storage definition. To determine that, the entity type and bundle
  99. // must be known, which is contextual information that the Symfony
  100. // serializer does not pass to ::supportsDenormalization().
  101. if (!is_array($data)) {
  102. $data = [$field_item->getDataDefinition()->getMainPropertyName() => $data];
  103. }
  104. if ($this->pmDataHasStringForSerializeColumn($field_item, $data)) {
  105. $field_name = $field_item->getParent() ? $field_item->getParent()->getName() : $field_item->getName();
  106. throw new \LogicException(sprintf('The generic FieldItemNormalizer cannot denormalize string values for "%s" properties of the "%s" field (field item class: %s).', implode('", "', $this->pmGetSerializedPropertyNames($field_item)), $field_name, $class));
  107. }
  108. }
  109.  
  110. /**
  111. * Checks if the data contains string value for serialize column.
  112. *
  113. * @param \Drupal\Core\Field\FieldItemInterface $field_item
  114. * The field item.
  115. * @param array $data
  116. * The data being denormalized.
  117. *
  118. * @return bool
  119. * TRUE if there is a string value for serialize column, otherwise FALSE.
  120. */
  121. protected function pmDataHasStringForSerializeColumn(FieldItemInterface $field_item, array $data) {
  122. error_log('pmDataHasStringForSerializeColumn');
  123. foreach ($this->pmGetSerializedPropertyNames($field_item) as $property_name) {
  124. if (isset($data[$property_name]) && is_string($data[$property_name])) {
  125. return TRUE;
  126. }
  127. }
  128. return FALSE;
  129. }
  130.  
  131. /**
  132. * Gets the names of all serialized properties.
  133. *
  134. * @param \Drupal\Core\Field\FieldItemInterface $field_item
  135. * The field item.
  136. *
  137. * @return string[]
  138. * The property names for serialized properties.
  139. */
  140. protected function pmGetSerializedPropertyNames(FieldItemInterface $field_item) {
  141. error_log('pmGetSerializedPropertyNames');
  142. $field_storage_definition = $field_item->getFieldDefinition()->getFieldStorageDefinition();
  143.  
  144. if ($custom_property_names = $this->pmGetCustomSerializedPropertyNames($field_item)) {
  145. return $custom_property_names;
  146. }
  147.  
  148. $field_storage_schema = $field_item->schema($field_storage_definition);
  149. // If there are no columns then there are no serialized properties.
  150. if (!isset($field_storage_schema['columns'])) {
  151. return [];
  152. }
  153. $serialized_columns = array_filter($field_storage_schema['columns'], function ($column_schema) {
  154. return isset($column_schema['serialize']) && $column_schema['serialize'] === TRUE;
  155. });
  156. return array_keys($serialized_columns);
  157. }
  158.  
  159. /**
  160. * Gets the names of all properties the plugin treats as serialized data.
  161. *
  162. * This allows the field storage definition or entity type to provide a
  163. * setting for serialized properties. This can be used for fields that
  164. * handle serialized data themselves and do not rely on the serialized schema
  165. * flag.
  166. *
  167. * @param \Drupal\Core\Field\FieldItemInterface $field_item
  168. * The field item.
  169. *
  170. * @return string[]
  171. * The property names for serialized properties.
  172. */
  173. protected function pmGetCustomSerializedPropertyNames(FieldItemInterface $field_item) {
  174. error_log('pmGetCustomSerializedPropertyNames');
  175. if ($field_item instanceof PluginInspectionInterface) {
  176. $definition = $field_item->getPluginDefinition();
  177. $serialized_fields = $field_item->getEntity()->getEntityType()->get('serialized_field_property_names');
  178. $field_name = $field_item->getFieldDefinition()->getName();
  179. if (is_array($serialized_fields) && isset($serialized_fields[$field_name]) && is_array($serialized_fields[$field_name])) {
  180. return $serialized_fields[$field_name];
  181. }
  182. if (isset($definition['serialized_property_names']) && is_array($definition['serialized_property_names'])) {
  183. return $definition['serialized_property_names'];
  184. }
  185. }
  186. return [];
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement