3 namespace Drupal\Core\TypedData;
6 * A typed data definition class for defining data based on defined data types.
8 class DataDefinition implements DataDefinitionInterface, \ArrayAccess {
13 * The array holding values for all definition keys.
17 protected $definition = [];
20 * Creates a new data definition.
23 * The data type of the data; e.g., 'string', 'integer' or 'any'.
26 * A new DataDefinition object.
28 public static function create($type) {
29 $definition['type'] = $type;
30 return new static($definition);
36 public static function createFromDataType($type) {
37 return self::create($type);
41 * Constructs a new data definition object.
43 * @param array $values
44 * (optional) If given, an array of initial values to set on the definition.
46 public function __construct(array $values = []) {
47 $this->definition = $values;
53 public function getDataType() {
54 return !empty($this->definition['type']) ? $this->definition['type'] : 'any';
61 * The data type to set.
64 * The object itself for chaining.
66 public function setDataType($type) {
67 $this->definition['type'] = $type;
74 public function getLabel() {
75 return isset($this->definition['label']) ? $this->definition['label'] : NULL;
79 * Sets the human-readable label.
81 * @param string $label
85 * The object itself for chaining.
87 public function setLabel($label) {
88 $this->definition['label'] = $label;
95 public function getDescription() {
96 return isset($this->definition['description']) ? $this->definition['description'] : NULL;
100 * Sets the human-readable description.
102 * @param string $description
103 * The description to set.
106 * The object itself for chaining.
108 public function setDescription($description) {
109 $this->definition['description'] = $description;
116 public function isList() {
117 return ($this instanceof ListDataDefinitionInterface);
123 public function isReadOnly() {
124 if (!isset($this->definition['read-only'])) {
125 // Default to read-only if the data value is computed.
126 return $this->isComputed();
128 return !empty($this->definition['read-only']);
132 * Sets whether the data is read-only.
134 * @param bool $read_only
135 * Whether the data is read-only.
138 * The object itself for chaining.
140 public function setReadOnly($read_only) {
141 $this->definition['read-only'] = $read_only;
148 public function isComputed() {
149 return !empty($this->definition['computed']);
153 * Sets whether the data is computed.
155 * @param bool $computed
156 * Whether the data is computed.
159 * The object itself for chaining.
161 public function setComputed($computed) {
162 $this->definition['computed'] = $computed;
169 public function isRequired() {
170 return !empty($this->definition['required']);
174 * Sets whether the data is required.
176 * @param bool $required
177 * Whether the data is required.
180 * The object itself for chaining.
182 public function setRequired($required) {
183 $this->definition['required'] = $required;
190 public function getClass() {
191 if (isset($this->definition['class'])) {
192 return $this->definition['class'];
195 $type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
196 return $type_definition['class'];
201 * Sets the class used for creating the typed data object.
203 * @param string|null $class
207 * The object itself for chaining.
209 public function setClass($class) {
210 $this->definition['class'] = $class;
217 public function getSettings() {
218 return isset($this->definition['settings']) ? $this->definition['settings'] : [];
222 * Sets the array of settings, as required by the used class.
224 * @param array $settings
225 * The array of settings.
228 * The object itself for chaining.
230 public function setSettings(array $settings) {
231 $this->definition['settings'] = $settings;
238 public function getSetting($setting_name) {
239 return isset($this->definition['settings'][$setting_name]) ? $this->definition['settings'][$setting_name] : NULL;
243 * Sets a definition setting.
245 * @param string $setting_name
246 * The definition setting to set.
247 * @param mixed $value
251 * The object itself for chaining.
253 public function setSetting($setting_name, $value) {
254 $this->definition['settings'][$setting_name] = $value;
261 public function getConstraints() {
262 $constraints = isset($this->definition['constraints']) ? $this->definition['constraints'] : [];
263 $constraints += $this->getTypedDataManager()->getDefaultConstraints($this);
270 public function getConstraint($constraint_name) {
271 $constraints = $this->getConstraints();
272 return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
276 * Sets an array of validation constraints.
278 * @param array $constraints
279 * An array of validation constraint definitions, keyed by constraint name.
280 * Each constraint definition can be used for instantiating
281 * \Symfony\Component\Validator\Constraint objects.
285 public function setConstraints(array $constraints) {
286 $this->definition['constraints'] = $constraints;
293 public function addConstraint($constraint_name, $options = NULL) {
294 $this->definition['constraints'][$constraint_name] = $options;
301 * This is for BC support only.
302 * @todo: Remove in https://www.drupal.org/node/1928868.
304 public function offsetExists($offset) {
305 // PHP's array access does not work correctly with isset(), so we have to
306 // bake isset() in here. See https://bugs.php.net/bug.php?id=41727.
307 return array_key_exists($offset, $this->definition) && isset($this->definition[$offset]);
313 * This is for BC support only.
314 * @todo: Remove in https://www.drupal.org/node/1928868.
316 public function &offsetGet($offset) {
317 if (!isset($this->definition[$offset])) {
318 $this->definition[$offset] = NULL;
320 return $this->definition[$offset];
326 * This is for BC support only.
327 * @todo: Remove in https://www.drupal.org/node/1928868.
329 public function offsetSet($offset, $value) {
330 $this->definition[$offset] = $value;
336 * This is for BC support only.
337 * @todo: Remove in https://www.drupal.org/node/1928868.
339 public function offsetUnset($offset) {
340 unset($this->definition[$offset]);
344 * Returns all definition values as array.
348 public function toArray() {
349 return $this->definition;
355 public function __sleep() {
356 // Never serialize the typed data manager.
357 $vars = get_object_vars($this);
358 unset($vars['typedDataManager']);
359 return array_keys($vars);
365 public function isInternal() {
366 // Respect the definition, otherwise default to TRUE for computed fields.
367 if (isset($this->definition['internal'])) {
368 return $this->definition['internal'];
370 return $this->isComputed();
374 * Sets the whether the data value should be internal.
376 * @param bool $internal
377 * Whether the data value should be internal.
381 public function setInternal($internal) {
382 $this->definition['internal'] = $internal;