TypedDataInterface.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * The data value.
  42. */
  43. public function getValue();
  44. /**
  45. * Sets the data value.
  46. *
  47. * @param mixed|null $value
  48. * The value to set in the format as documented for the data type or NULL to
  49. * unset the data value.
  50. * @param bool $notify
  51. * (optional) Whether to notify the parent object of the change. Defaults to
  52. * TRUE. If a property is updated from a parent object, set it to FALSE to
  53. * avoid being notified again.
  54. *
  55. * @throws \InvalidArgumentException
  56. * If the value input is inappropriate.
  57. * @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
  58. * If the data is read-only.
  59. */
  60. public function setValue($value, $notify = TRUE);
  61. /**
  62. * Returns a string representation of the data.
  63. *
  64. * @return string
  65. * The string representation of the data.
  66. */
  67. public function getString();
  68. /**
  69. * Gets a list of validation constraints.
  70. *
  71. * @return array
  72. * Array of constraints, each being an instance of
  73. * \Symfony\Component\Validator\Constraint.
  74. */
  75. public function getConstraints();
  76. /**
  77. * Validates the currently set data value.
  78. *
  79. * @return \Symfony\Component\Validator\ConstraintViolationListInterface
  80. * A list of constraint violations. If the list is empty, validation
  81. * succeeded.
  82. */
  83. public function validate();
  84. /**
  85. * Applies the default value.
  86. *
  87. * @param bool $notify
  88. * (optional) Whether to notify the parent object of the change. Defaults to
  89. * TRUE. If a property is updated from a parent object, set it to FALSE to
  90. * avoid being notified again.
  91. *
  92. * @return $this
  93. * Returns itself to allow for chaining.
  94. */
  95. public function applyDefaultValue($notify = TRUE);
  96. /**
  97. * Returns the name of a property or item.
  98. *
  99. * @return string|int|null
  100. * If the data is a property of some complex data, the name of the property.
  101. * If the data is an item of a list, the name is the numeric position of the
  102. * item in the list, starting with 0. Otherwise, NULL is returned.
  103. */
  104. public function getName();
  105. /**
  106. * Returns the parent data structure; i.e. either complex data or a list.
  107. *
  108. * @return \Drupal\Core\TypedData\TraversableTypedDataInterface|null
  109. * The parent data structure, either complex data or a list; or NULL if this
  110. * is the root of the typed data tree.
  111. */
  112. public function getParent();
  113. /**
  114. * Returns the root of the typed data tree.
  115. *
  116. * Returns the root data for a tree of typed data objects; e.g. for an entity
  117. * field item the root of the tree is its parent entity object.
  118. *
  119. * @return \Drupal\Core\TypedData\TraversableTypedDataInterface
  120. * The root data structure, either complex data or a list.
  121. */
  122. public function getRoot();
  123. /**
  124. * Returns the property path of the data.
  125. *
  126. * The trail of property names relative to the root of the typed data tree,
  127. * separated by dots; e.g. 'field_text.0.format'.
  128. *
  129. * @return string
  130. * The property path relative to the root of the typed tree, or an empty
  131. * string if this is the root.
  132. */
  133. public function getPropertyPath();
  134. /**
  135. * Sets the context of a property or item via a context aware parent.
  136. *
  137. * This method is supposed to be called by the factory only.
  138. *
  139. * @param string|null $name
  140. * (optional) The name of the property or the delta of the list item,
  141. * or NULL if it is the root of a typed data tree. Defaults to NULL.
  142. * @param \Drupal\Core\TypedData\TraversableTypedDataInterface|null $parent
  143. * (optional) The parent object of the data property, or NULL if it is the
  144. * root of a typed data tree. Defaults to NULL.
  145. */
  146. public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL);
  147. }