TypedDataManagerInterface.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. use Drupal\Component\Plugin\PluginManagerInterface;
  4. use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
  5. use Drupal\Core\Validation\ConstraintManager;
  6. use Symfony\Component\Validator\Validator\ValidatorInterface;
  7. /**
  8. * Defines an interface for typed data manager.
  9. */
  10. interface TypedDataManagerInterface extends PluginManagerInterface, CachedDiscoveryInterface {
  11. /**
  12. * Instantiates a typed data object.
  13. *
  14. * @param string $data_type
  15. * The data type, for which a typed object should be instantiated.
  16. * @param array $configuration
  17. * The plugin configuration array, i.e. an array with the following keys:
  18. * - data_definition: The data definition object, i.e. an instance of
  19. * \Drupal\Core\TypedData\DataDefinitionInterface.
  20. * - name: The name of the property or the delta of the list item if a
  21. * property or list item is to be created. Otherwise, this should be set
  22. * to NULL, but the key must be specified.
  23. * - parent: The parent typed data object implementing either the
  24. * ListInterface or the ComplexDataInterface if a property or list item is
  25. * to be created. Otherwise, this should be set to NULL, but the key must
  26. * be specified.
  27. *
  28. * @return \Drupal\Core\TypedData\TypedDataInterface
  29. * The instantiated typed data object.
  30. *
  31. * @see \Drupal\Core\TypedData\TypedDataManager::create()
  32. */
  33. public function createInstance($data_type, array $configuration = []);
  34. /**
  35. * Creates a new typed data object instance.
  36. *
  37. * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
  38. * The data definition of the typed data object. For backwards-compatibility
  39. * an array representation of the data definition may be passed also.
  40. * @param mixed $value
  41. * (optional) The data value. If set, it has to match one of the supported
  42. * data type format as documented for the data type classes.
  43. * @param string $name
  44. * (optional) If a property or list item is to be created, the name of the
  45. * property or the delta of the list item.
  46. * @param mixed $parent
  47. * (optional) If a property or list item is to be created, the parent typed
  48. * data object implementing either the ListInterface or the
  49. * ComplexDataInterface.
  50. *
  51. * @return \Drupal\Core\TypedData\TypedDataInterface
  52. * The instantiated typed data object.
  53. *
  54. * @see \Drupal\Core\TypedData\TypedDataManager::getPropertyInstance()
  55. * @see \Drupal\Core\TypedData\Plugin\DataType\BinaryData
  56. * @see \Drupal\Core\TypedData\Plugin\DataType\BooleanData
  57. * @see \Drupal\Core\TypedData\Plugin\DataType\Date
  58. * @see \Drupal\Core\TypedData\Plugin\DataType\Duration
  59. * @see \Drupal\Core\TypedData\Plugin\DataType\FloatData
  60. * @see \Drupal\Core\TypedData\Plugin\DataType\IntegerData
  61. * @see \Drupal\Core\TypedData\Plugin\DataType\StringData
  62. * @see \Drupal\Core\TypedData\Plugin\DataType\Uri
  63. */
  64. public function create(DataDefinitionInterface $definition, $value = NULL, $name = NULL, $parent = NULL);
  65. /**
  66. * Creates a new data definition object.
  67. *
  68. * While data definitions objects may be created directly if the definition
  69. * class used by a data type is known, this method allows the creation of data
  70. * definitions for any given data type.
  71. *
  72. * For example, if a definition for a map is to be created, the following code
  73. * could be used instead of calling this method with the argument 'map':
  74. * @code
  75. * $map_definition = \Drupal\Core\TypedData\MapDataDefinition::create();
  76. * @endcode
  77. *
  78. * @param string $data_type
  79. * The data type plugin ID, for which a data definition object should be
  80. * created.
  81. *
  82. * @return \Drupal\Core\TypedData\DataDefinitionInterface
  83. * A data definition object for the given data type. The class of this
  84. * object is provided by the definition_class in the plugin annotation.
  85. *
  86. * @see \Drupal\Core\TypedData\TypedDataManager::createListDataDefinition()
  87. */
  88. public function createDataDefinition($data_type);
  89. /**
  90. * Creates a new list data definition for items of the given data type.
  91. *
  92. * @param string $item_type
  93. * The item type, for which a list data definition should be created.
  94. *
  95. * @return \Drupal\Core\TypedData\ListDataDefinitionInterface
  96. * A list definition for items of the given data type.
  97. *
  98. * @see \Drupal\Core\TypedData\TypedDataManager::createDataDefinition()
  99. */
  100. public function createListDataDefinition($item_type);
  101. /**
  102. * {@inheritdoc}
  103. *
  104. * @param array $options
  105. * An array of options with the following keys:
  106. * - object: The parent typed data object, implementing the
  107. * TypedDataInterface and either the ListInterface or the
  108. * ComplexDataInterface.
  109. * - property: The name of the property to instantiate, or the delta of the
  110. * the list item to instantiate.
  111. * - value: The value to set. If set, it has to match one of the supported
  112. * data type formats as documented by the data type classes.
  113. *
  114. * @return \Drupal\Core\TypedData\TypedDataInterface
  115. * The new property instance.
  116. *
  117. * @throws \InvalidArgumentException
  118. * If the given property is not known, or the passed object does not
  119. * implement the ListInterface or the ComplexDataInterface.
  120. *
  121. * @see \Drupal\Core\TypedData\TypedDataManager::getPropertyInstance()
  122. */
  123. public function getInstance(array $options);
  124. /**
  125. * Get a typed data instance for a property of a given typed data object.
  126. *
  127. * This method will use prototyping for fast and efficient instantiation of
  128. * many property objects with the same property path; for example,
  129. * when multiple comments are used comment_body.0.value needs to be
  130. * instantiated very often.
  131. *
  132. * Prototyping is done by the root object's data type and the given
  133. * property path, i.e. all property instances having the same property path
  134. * and inheriting from the same data type are prototyped.
  135. *
  136. * @param \Drupal\Core\TypedData\TypedDataInterface $object
  137. * The parent typed data object, implementing the TypedDataInterface and
  138. * either the ListInterface or the ComplexDataInterface.
  139. * @param string $property_name
  140. * The name of the property to instantiate, or the delta of an list item.
  141. * @param mixed $value
  142. * (optional) The data value. If set, it has to match one of the supported
  143. * data type formats as documented by the data type classes.
  144. *
  145. * @return \Drupal\Core\TypedData\TypedDataInterface
  146. * The new property instance.
  147. *
  148. * @throws \InvalidArgumentException
  149. * If the given property is not known, or the passed object does not
  150. * implement the ListInterface or the ComplexDataInterface.
  151. *
  152. * @see \Drupal\Core\TypedData\TypedDataManager::create()
  153. */
  154. public function getPropertyInstance(TypedDataInterface $object, $property_name, $value = NULL);
  155. /**
  156. * Gets the validator for validating typed data.
  157. *
  158. * @return \Symfony\Component\Validator\Validator\ValidatorInterface
  159. * The validator object.
  160. */
  161. public function getValidator();
  162. /**
  163. * Sets the validator for validating typed data.
  164. *
  165. * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
  166. * The validator object to set.
  167. */
  168. public function setValidator(ValidatorInterface $validator);
  169. /**
  170. * Gets the validation constraint manager.
  171. *
  172. * @return \Drupal\Core\Validation\ConstraintManager
  173. * The constraint manager.
  174. */
  175. public function getValidationConstraintManager();
  176. /**
  177. * Sets the validation constraint manager.
  178. *
  179. * The validation constraint manager is used to instantiate validation
  180. * constraint plugins.
  181. *
  182. * @param \Drupal\Core\Validation\ConstraintManager $constraintManager
  183. * The constraint manager to set.
  184. */
  185. public function setValidationConstraintManager(ConstraintManager $constraintManager);
  186. /**
  187. * Gets default constraints for the given data definition.
  188. *
  189. * This generates default constraint definitions based on the data definition;
  190. * for example, a NotNull constraint is generated if the data is defined as
  191. * required. Besides that, any constraints defined for the data type (that is,
  192. * below the 'constraint' key of the type's plugin definition) are taken into
  193. * account.
  194. *
  195. * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
  196. * A data definition.
  197. *
  198. * @return array
  199. * An array of validation constraint definitions, keyed by constraint name.
  200. * Each constraint definition can be used for instantiating
  201. * \Symfony\Component\Validator\Constraint objects.
  202. */
  203. public function getDefaultConstraints(DataDefinitionInterface $definition);
  204. /**
  205. * Gets the canonical representation of a TypedData object.
  206. *
  207. * The canonical representation is typically used when data is passed on to
  208. * other code components. In many use cases, the TypedData object is mostly
  209. * unified adapter wrapping a primary value (a string, an entity, etc.) which
  210. * is the canonical representation that consuming code like constraint
  211. * validators are really interested in. For some APIs, though, the domain
  212. * object (for example, Field API's FieldItem and FieldItemList) directly
  213. * implements TypedDataInterface, and the canonical representation is thus the
  214. * data object itself.
  215. *
  216. * When a TypedData object gets validated, for example, its canonical
  217. * representation is passed on to constraint validators, which thus receive
  218. * an Entity unwrapped, but a FieldItem as is.
  219. *
  220. * Data types specify whether their data objects need unwrapping by using the
  221. * 'unwrap_for_canonical_representation' property in the data definition
  222. * (defaults to TRUE).
  223. *
  224. * @param \Drupal\Core\TypedData\TypedDataInterface $data
  225. * The data.
  226. *
  227. * @return mixed
  228. * The canonical representation of the passed data.
  229. */
  230. public function getCanonicalRepresentation(TypedDataInterface $data);
  231. }