3 namespace Drupal\Core\TypedData;
6 * Interface for typed data objects.
8 * @see \Drupal\Core\TypedData\DataDefinitionInterface
12 interface TypedDataInterface {
15 * Constructs a TypedData object given its definition and context.
17 * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
18 * The data definition.
19 * @param string|null $name
20 * (optional) The name of the created property, or NULL if it is the root
21 * of a typed data tree. Defaults to NULL.
22 * @param \Drupal\Core\TypedData\TraversableTypedDataInterface $parent
23 * (optional) The parent object of the data property, or NULL if it is the
24 * root of a typed data tree. Defaults to NULL.
26 * @todo When \Drupal\Core\Config\TypedConfigManager has been fixed to use
27 * class-based definitions, type-hint $definition to
28 * DataDefinitionInterface. https://www.drupal.org/node/1928868
30 * @see \Drupal\Core\TypedData\TypedDataManager::create()
32 public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL);
35 * Gets the data definition.
37 * @return \Drupal\Core\TypedData\DataDefinitionInterface
38 * The data definition object.
40 public function getDataDefinition();
43 * Gets the data value.
47 public function getValue();
50 * Sets the data value.
52 * @param mixed|null $value
53 * The value to set in the format as documented for the data type or NULL to
54 * unset the data value.
56 * (optional) Whether to notify the parent object of the change. Defaults to
57 * TRUE. If a property is updated from a parent object, set it to FALSE to
58 * avoid being notified again.
60 * @throws \InvalidArgumentException
61 * If the value input is inappropriate.
62 * @throws \Drupal\Core\TypedData\Exception\ReadOnlyException
63 * If the data is read-only.
65 public function setValue($value, $notify = TRUE);
68 * Returns a string representation of the data.
72 public function getString();
75 * Gets a list of validation constraints.
78 * Array of constraints, each being an instance of
79 * \Symfony\Component\Validator\Constraint.
81 public function getConstraints();
84 * Validates the currently set data value.
86 * @return \Symfony\Component\Validator\ConstraintViolationListInterface
87 * A list of constraint violations. If the list is empty, validation
90 public function validate();
93 * Applies the default value.
96 * (optional) Whether to notify the parent object of the change. Defaults to
97 * TRUE. If a property is updated from a parent object, set it to FALSE to
98 * avoid being notified again.
100 * @return \Drupal\Core\TypedData\TypedDataInterface
101 * Returns itself to allow for chaining.
103 public function applyDefaultValue($notify = TRUE);
106 * Returns the name of a property or item.
109 * If the data is a property of some complex data, the name of the property.
110 * If the data is an item of a list, the name is the numeric position of the
111 * item in the list, starting with 0. Otherwise, NULL is returned.
113 public function getName();
116 * Returns the parent data structure; i.e. either complex data or a list.
118 * @return \Drupal\Core\TypedData\TraversableTypedDataInterface|null
119 * The parent data structure, either complex data or a list; or NULL if this
120 * is the root of the typed data tree.
122 public function getParent();
125 * Returns the root of the typed data tree.
127 * Returns the root data for a tree of typed data objects; e.g. for an entity
128 * field item the root of the tree is its parent entity object.
130 * @return \Drupal\Core\TypedData\TraversableTypedDataInterface
131 * The root data structure, either complex data or a list.
133 public function getRoot();
136 * Returns the property path of the data.
138 * The trail of property names relative to the root of the typed data tree,
139 * separated by dots; e.g. 'field_text.0.format'.
142 * The property path relative to the root of the typed tree, or an empty
143 * string if this is the root.
145 public function getPropertyPath();
148 * Sets the context of a property or item via a context aware parent.
150 * This method is supposed to be called by the factory only.
152 * @param string|null $name
153 * (optional) The name of the property or the delta of the list item,
154 * or NULL if it is the root of a typed data tree. Defaults to NULL.
155 * @param \Drupal\Core\TypedData\TraversableTypedDataInterface|null $parent
156 * (optional) The parent object of the data property, or NULL if it is the
157 * root of a typed data tree. Defaults to NULL.
159 public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL);