DataDefinitionInterface.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * Interface for data definitions.
  5. *
  6. * Data definitions are used to describe data based upon available data types.
  7. * For example, a plugin could describe its parameters using data definitions
  8. * in order to specify what kind of data is required for it.
  9. *
  10. * Definitions that describe lists or complex data have to implement the
  11. * respective interfaces, such that the metadata about contained list items or
  12. * properties can be retrieved from the definition.
  13. *
  14. * @see \Drupal\Core\TypedData\DataDefinition
  15. * @see \Drupal\Core\TypedData\ListDefinitionInterface
  16. * @see \Drupal\Core\TypedData\ComplexDataDefinitionInterface
  17. * @see \Drupal\Core\TypedData\DataReferenceDefinitionInterface
  18. * @see \Drupal\Core\TypedData\TypedDataInterface
  19. *
  20. * @ingroup typed_data
  21. */
  22. interface DataDefinitionInterface {
  23. /**
  24. * Creates a new data definition object.
  25. *
  26. * This method is typically used by
  27. * \Drupal\Core\TypedData\TypedDataManager::createDataDefinition() to build a
  28. * definition object for an arbitrary data type. When the definition class is
  29. * known, it is recommended to directly use the static create() method on that
  30. * class instead; e.g.:
  31. * @code
  32. * $map_definition = \Drupal\Core\TypedData\MapDataDefinition::create();
  33. * @endcode
  34. *
  35. * @param string $data_type
  36. * The data type, for which a data definition should be created.
  37. *
  38. * @return static
  39. *
  40. * @throws \InvalidArgumentException
  41. * If an unsupported data type gets passed to the class; e.g., 'string' to a
  42. * definition class handling 'entity:* data types.
  43. */
  44. public static function createFromDataType($data_type);
  45. /**
  46. * Returns the data type of the data.
  47. *
  48. * @return string
  49. * The data type.
  50. */
  51. public function getDataType();
  52. /**
  53. * Returns a human readable label.
  54. *
  55. * @return string|\Drupal\Core\StringTranslation\TranslatableMarkup
  56. * The label. A string or an instance of TranslatableMarkup will be returned
  57. * based on the way the label translation is handled.
  58. */
  59. public function getLabel();
  60. /**
  61. * Returns a human readable description.
  62. *
  63. * Descriptions are usually used on user interfaces where the data is edited
  64. * or displayed.
  65. *
  66. * @return string|null
  67. * The description, or NULL if no description is available.
  68. */
  69. public function getDescription();
  70. /**
  71. * Returns whether the data is multi-valued, i.e. a list of data items.
  72. *
  73. * This is equivalent to checking whether the data definition implements the
  74. * \Drupal\Core\TypedData\ListDefinitionInterface interface.
  75. *
  76. * @return bool
  77. * Whether the data is multi-valued.
  78. */
  79. public function isList();
  80. /**
  81. * Determines whether the data is read-only.
  82. *
  83. * @return bool
  84. * Whether the data is read-only.
  85. */
  86. public function isReadOnly();
  87. /**
  88. * Determines whether the data value is computed.
  89. *
  90. * For example, data could be computed depending on some other values.
  91. *
  92. * @return bool
  93. * Whether the data value is computed.
  94. */
  95. public function isComputed();
  96. /**
  97. * Determines whether a data value is required.
  98. *
  99. * For required data a non-NULL value is mandatory.
  100. *
  101. * @return bool
  102. * Whether a data value is required.
  103. */
  104. public function isRequired();
  105. /**
  106. * Returns the class used for creating the typed data object.
  107. *
  108. * If not specified, the default class of the data type will be returned.
  109. *
  110. * @return string
  111. * The class used for creating the typed data object.
  112. */
  113. public function getClass();
  114. /**
  115. * Returns the array of settings, as required by the used class.
  116. *
  117. * See the documentation of the class for supported or required settings.
  118. *
  119. * @return array
  120. * The array of settings.
  121. */
  122. public function getSettings();
  123. /**
  124. * Returns the value of a given setting.
  125. *
  126. * @param string $setting_name
  127. * The setting name.
  128. *
  129. * @return mixed
  130. * The setting value.
  131. */
  132. public function getSetting($setting_name);
  133. /**
  134. * Returns an array of validation constraints.
  135. *
  136. * The validation constraints of a definition consist of any for it defined
  137. * constraints and default constraints, which are generated based on the
  138. * definition and its data type. See
  139. * \Drupal\Core\TypedData\TypedDataManager::getDefaultConstraints().
  140. *
  141. * Constraints are defined via an array, having constraint plugin IDs as key
  142. * and constraint options as values, e.g.
  143. * @code
  144. * $constraints = array(
  145. * 'Range' => array('min' => 5, 'max' => 10),
  146. * 'NotBlank' => array(),
  147. * );
  148. * @endcode
  149. * Options have to be specified using another array if the constraint has more
  150. * than one or zero options. If it has exactly one option, the value should be
  151. * specified without nesting it into another array:
  152. * @code
  153. * $constraints = array(
  154. * 'EntityType' => 'node',
  155. * 'Bundle' => 'article',
  156. * );
  157. * @endcode
  158. *
  159. * Note that the specified constraints must be compatible with the data type,
  160. * e.g. for data of type 'entity' the 'EntityType' and 'Bundle' constraints
  161. * may be specified.
  162. *
  163. * @see \Drupal\Core\Validation\ConstraintManager
  164. *
  165. * @return array[]
  166. * An array of validation constraint definitions, keyed by constraint name.
  167. * Each constraint definition can be used for instantiating
  168. * \Symfony\Component\Validator\Constraint objects.
  169. *
  170. * @see \Symfony\Component\Validator\Constraint
  171. */
  172. public function getConstraints();
  173. /**
  174. * Returns a validation constraint.
  175. *
  176. * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
  177. * details.
  178. *
  179. * @param string $constraint_name
  180. * The name of the constraint, i.e. its plugin id.
  181. *
  182. * @return array
  183. * A validation constraint definition which can be used for instantiating a
  184. * \Symfony\Component\Validator\Constraint object.
  185. *
  186. * @see \Symfony\Component\Validator\Constraint
  187. */
  188. public function getConstraint($constraint_name);
  189. /**
  190. * Adds a validation constraint.
  191. *
  192. * See \Drupal\Core\TypedData\DataDefinitionInterface::getConstraints() for
  193. * details.
  194. *
  195. * @param string $constraint_name
  196. * The name of the constraint to add, i.e. its plugin id.
  197. * @param array|null $options
  198. * The constraint options as required by the constraint plugin, or NULL.
  199. *
  200. * @return static
  201. * The object itself for chaining.
  202. */
  203. public function addConstraint($constraint_name, $options = NULL);
  204. /**
  205. * Determines whether the data value is internal.
  206. *
  207. * This can be used in a scenario when it is not desirable to expose this data
  208. * value to an external system.
  209. *
  210. * @return bool
  211. * Whether the data value is internal.
  212. */
  213. public function isInternal();
  214. }