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 {
11 * The array holding values for all definition keys.
15 protected $definition = [];
18 * Creates a new data definition.
21 * The data type of the data; e.g., 'string', 'integer' or 'any'.
24 * A new DataDefinition object.
26 public static function create($type) {
27 $definition['type'] = $type;
28 return new static($definition);
34 public static function createFromDataType($type) {
35 return self::create($type);
39 * Constructs a new data definition object.
41 * @param array $values
42 * (optional) If given, an array of initial values to set on the definition.
44 public function __construct(array $values = []) {
45 $this->definition = $values;
51 public function getDataType() {
52 return !empty($this->definition['type']) ? $this->definition['type'] : 'any';
59 * The data type to set.
62 * The object itself for chaining.
64 public function setDataType($type) {
65 $this->definition['type'] = $type;
72 public function getLabel() {
73 return isset($this->definition['label']) ? $this->definition['label'] : NULL;
77 * Sets the human-readable label.
79 * @param string $label
83 * The object itself for chaining.
85 public function setLabel($label) {
86 $this->definition['label'] = $label;
93 public function getDescription() {
94 return isset($this->definition['description']) ? $this->definition['description'] : NULL;
98 * Sets the human-readable description.
100 * @param string $description
101 * The description to set.
104 * The object itself for chaining.
106 public function setDescription($description) {
107 $this->definition['description'] = $description;
114 public function isList() {
115 return ($this instanceof ListDataDefinitionInterface);
121 public function isReadOnly() {
122 if (!isset($this->definition['read-only'])) {
123 // Default to read-only if the data value is computed.
124 return $this->isComputed();
126 return !empty($this->definition['read-only']);
130 * Sets whether the data is read-only.
132 * @param bool $read_only
133 * Whether the data is read-only.
136 * The object itself for chaining.
138 public function setReadOnly($read_only) {
139 $this->definition['read-only'] = $read_only;
146 public function isComputed() {
147 return !empty($this->definition['computed']);
151 * Sets whether the data is computed.
153 * @param bool $computed
154 * Whether the data is computed.
157 * The object itself for chaining.
159 public function setComputed($computed) {
160 $this->definition['computed'] = $computed;
167 public function isRequired() {
168 return !empty($this->definition['required']);
172 * Sets whether the data is required.
174 * @param bool $required
175 * Whether the data is required.
178 * The object itself for chaining.
180 public function setRequired($required) {
181 $this->definition['required'] = $required;
188 public function getClass() {
189 if (isset($this->definition['class'])) {
190 return $this->definition['class'];
193 $type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
194 return $type_definition['class'];
199 * Sets the class used for creating the typed data object.
201 * @param string|null $class
205 * The object itself for chaining.
207 public function setClass($class) {
208 $this->definition['class'] = $class;
215 public function getSettings() {
216 return isset($this->definition['settings']) ? $this->definition['settings'] : [];
220 * Sets the array of settings, as required by the used class.
222 * @param array $settings
223 * The array of settings.
226 * The object itself for chaining.
228 public function setSettings(array $settings) {
229 $this->definition['settings'] = $settings;
236 public function getSetting($setting_name) {
237 return isset($this->definition['settings'][$setting_name]) ? $this->definition['settings'][$setting_name] : NULL;
241 * Sets a definition setting.
243 * @param string $setting_name
244 * The definition setting to set.
245 * @param mixed $value
249 * The object itself for chaining.
251 public function setSetting($setting_name, $value) {
252 $this->definition['settings'][$setting_name] = $value;
259 public function getConstraints() {
260 $constraints = isset($this->definition['constraints']) ? $this->definition['constraints'] : [];
261 $constraints += \Drupal::typedDataManager()->getDefaultConstraints($this);
268 public function getConstraint($constraint_name) {
269 $constraints = $this->getConstraints();
270 return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
276 public function setConstraints(array $constraints) {
277 $this->definition['constraints'] = $constraints;
284 public function addConstraint($constraint_name, $options = NULL) {
285 $this->definition['constraints'][$constraint_name] = $options;
292 * This is for BC support only.
293 * @todo: Remove in https://www.drupal.org/node/1928868.
295 public function offsetExists($offset) {
296 // PHP's array access does not work correctly with isset(), so we have to
297 // bake isset() in here. See https://bugs.php.net/bug.php?id=41727.
298 return array_key_exists($offset, $this->definition) && isset($this->definition[$offset]);
304 * This is for BC support only.
305 * @todo: Remove in https://www.drupal.org/node/1928868.
307 public function &offsetGet($offset) {
308 if (!isset($this->definition[$offset])) {
309 $this->definition[$offset] = NULL;
311 return $this->definition[$offset];
317 * This is for BC support only.
318 * @todo: Remove in https://www.drupal.org/node/1928868.
320 public function offsetSet($offset, $value) {
321 $this->definition[$offset] = $value;
327 * This is for BC support only.
328 * @todo: Remove in https://www.drupal.org/node/1928868.
330 public function offsetUnset($offset) {
331 unset($this->definition[$offset]);
335 * Returns all definition values as array.
339 public function toArray() {
340 return $this->definition;