TypedDataInterface.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * Interface for typed data objects.
  5. *
  6. * @see \Drupal\Core\TypedData\DataDefinitionInterface
  7. *
  8. * @ingroup typed_data
  9. */
  10. interface TypedDataInterface {
  11. /**
  12. * Constructs a TypedData object given its definition and context.
  13. *
  14. * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
  15. * The data definition.
  16. * @param string|null $name
  17. * (optional) The name of the created property, or NULL if it is the root
  18. * of a typed data tree. Defaults to NULL.
  19. * @param \Drupal\Core\TypedData\TraversableTypedDataInterface $parent
  20. * (optional) The parent object of the data property, or NULL if it is the
  21. * root of a typed data tree. Defaults to NULL.
  22. *
  23. * @todo When \Drupal\Core\Config\TypedConfigManager has been fixed to use
  24. * class-based definitions, type-hint $definition to
  25. * DataDefinitionInterface. https://www.drupal.org/node/1928868
  26. *
  27. * @see \Drupal\Core\TypedData\TypedDataManager::create()
  28. */
  29. public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL);
  30. /**
  31. * Gets the data definition.
  32. *
  33. * @return \Drupal\Core\TypedData\DataDefinitionInterface
  34. * The data definition object.
  35. */
  36. public function getDataDefinition();
  37. /**
  38. * Gets the data value.
  39. *
  40. * @return mixed
  41. */
  42. public function getValue();
  43. /**
  44. * Sets the data value.
  45. *
  46. * @param mixed|null $value
  47. * The value to set in the format as documented for the data type or NULL to
  48. * unset the data value.
  49. * @param bool $notify
  50. * (optional) Whether to notify the parent object of the change. Defaults to
  51. * TRUE. If a property is updated from a parent object, set it to FALSE to
  52. * avoid being notified again.
  53. *
  54. * @throws \InvalidArgumentException
  55. * If the value input is inappropriate.
  56. * @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
  57. * If the data is read-only.
  58. */
  59. public function setValue($value, $notify = TRUE);
  60. /**
  61. * Returns a string representation of the data.
  62. *
  63. * @return string
  64. */
  65. public function getString();
  66. /**
  67. * Gets a list of validation constraints.
  68. *
  69. * @return array
  70. * Array of constraints, each being an instance of
  71. * \Symfony\Component\Validator\Constraint.
  72. */
  73. public function getConstraints();
  74. /**
  75. * Validates the currently set data value.
  76. *
  77. * @return \Symfony\Component\Validator\ConstraintViolationListInterface
  78. * A list of constraint violations. If the list is empty, validation
  79. * succeeded.
  80. */
  81. public function validate();
  82. /**
  83. * Applies the default value.
  84. *
  85. * @param bool $notify
  86. * (optional) Whether to notify the parent object of the change. Defaults to
  87. * TRUE. If a property is updated from a parent object, set it to FALSE to
  88. * avoid being notified again.
  89. *
  90. * @return \Drupal\Core\TypedData\TypedDataInterface
  91. * Returns itself to allow for chaining.
  92. */
  93. public function applyDefaultValue($notify = TRUE);
  94. /**
  95. * Returns the name of a property or item.
  96. *
  97. * @return string
  98. * If the data is a property of some complex data, the name of the property.
  99. * If the data is an item of a list, the name is the numeric position of the
  100. * item in the list, starting with 0. Otherwise, NULL is returned.
  101. */
  102. public function getName();
  103. /**
  104. * Returns the parent data structure; i.e. either complex data or a list.
  105. *
  106. * @return \Drupal\Core\TypedData\TraversableTypedDataInterface|null
  107. * The parent data structure, either complex data or a list; or NULL if this
  108. * is the root of the typed data tree.
  109. */
  110. public function getParent();
  111. /**
  112. * Returns the root of the typed data tree.
  113. *
  114. * Returns the root data for a tree of typed data objects; e.g. for an entity
  115. * field item the root of the tree is its parent entity object.
  116. *
  117. * @return \Drupal\Core\TypedData\TraversableTypedDataInterface
  118. * The root data structure, either complex data or a list.
  119. */
  120. public function getRoot();
  121. /**
  122. * Returns the property path of the data.
  123. *
  124. * The trail of property names relative to the root of the typed data tree,
  125. * separated by dots; e.g. 'field_text.0.format'.
  126. *
  127. * @return string
  128. * The property path relative to the root of the typed tree, or an empty
  129. * string if this is the root.
  130. */
  131. public function getPropertyPath();
  132. /**
  133. * Sets the context of a property or item via a context aware parent.
  134. *
  135. * This method is supposed to be called by the factory only.
  136. *
  137. * @param string|null $name
  138. * (optional) The name of the property or the delta of the list item,
  139. * or NULL if it is the root of a typed data tree. Defaults to NULL.
  140. * @param \Drupal\Core\TypedData\TraversableTypedDataInterface|null $parent
  141. * (optional) The parent object of the data property, or NULL if it is the
  142. * root of a typed data tree. Defaults to NULL.
  143. */
  144. public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL);
  145. }