Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / TypedData / DataDefinition.php
1 <?php
2
3 namespace Drupal\Core\TypedData;
4
5 /**
6  * A typed data definition class for defining data based on defined data types.
7  */
8 class DataDefinition implements DataDefinitionInterface, \ArrayAccess {
9
10   use TypedDataTrait;
11
12   /**
13    * The array holding values for all definition keys.
14    *
15    * @var array
16    */
17   protected $definition = [];
18
19   /**
20    * Creates a new data definition.
21    *
22    * @param string $type
23    *   The data type of the data; e.g., 'string', 'integer' or 'any'.
24    *
25    * @return static
26    *   A new DataDefinition object.
27    */
28   public static function create($type) {
29     $definition['type'] = $type;
30     return new static($definition);
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public static function createFromDataType($type) {
37     return self::create($type);
38   }
39
40   /**
41    * Constructs a new data definition object.
42    *
43    * @param array $values
44    *   (optional) If given, an array of initial values to set on the definition.
45    */
46   public function __construct(array $values = []) {
47     $this->definition = $values;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getDataType() {
54     return !empty($this->definition['type']) ? $this->definition['type'] : 'any';
55   }
56
57   /**
58    * Sets the data type.
59    *
60    * @param string $type
61    *   The data type to set.
62    *
63    * @return static
64    *   The object itself for chaining.
65    */
66   public function setDataType($type) {
67     $this->definition['type'] = $type;
68     return $this;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getLabel() {
75     return isset($this->definition['label']) ? $this->definition['label'] : NULL;
76   }
77
78   /**
79    * Sets the human-readable label.
80    *
81    * @param string $label
82    *   The label to set.
83    *
84    * @return static
85    *   The object itself for chaining.
86    */
87   public function setLabel($label) {
88     $this->definition['label'] = $label;
89     return $this;
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function getDescription() {
96     return isset($this->definition['description']) ? $this->definition['description'] : NULL;
97   }
98
99   /**
100    * Sets the human-readable description.
101    *
102    * @param string $description
103    *   The description to set.
104    *
105    * @return static
106    *   The object itself for chaining.
107    */
108   public function setDescription($description) {
109     $this->definition['description'] = $description;
110     return $this;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function isList() {
117     return ($this instanceof ListDataDefinitionInterface);
118   }
119
120   /**
121    * {@inheritdoc}
122    */
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();
127     }
128     return !empty($this->definition['read-only']);
129   }
130
131   /**
132    * Sets whether the data is read-only.
133    *
134    * @param bool $read_only
135    *   Whether the data is read-only.
136    *
137    * @return static
138    *   The object itself for chaining.
139    */
140   public function setReadOnly($read_only) {
141     $this->definition['read-only'] = $read_only;
142     return $this;
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   public function isComputed() {
149     return !empty($this->definition['computed']);
150   }
151
152   /**
153    * Sets whether the data is computed.
154    *
155    * @param bool $computed
156    *   Whether the data is computed.
157    *
158    * @return static
159    *   The object itself for chaining.
160    */
161   public function setComputed($computed) {
162     $this->definition['computed'] = $computed;
163     return $this;
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function isRequired() {
170     return !empty($this->definition['required']);
171   }
172
173   /**
174    * Sets whether the data is required.
175    *
176    * @param bool $required
177    *   Whether the data is required.
178    *
179    * @return static
180    *   The object itself for chaining.
181    */
182   public function setRequired($required) {
183     $this->definition['required'] = $required;
184     return $this;
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   public function getClass() {
191     if (isset($this->definition['class'])) {
192       return $this->definition['class'];
193     }
194     else {
195       $type_definition = \Drupal::typedDataManager()->getDefinition($this->getDataType());
196       return $type_definition['class'];
197     }
198   }
199
200   /**
201    * Sets the class used for creating the typed data object.
202    *
203    * @param string|null $class
204    *   The class to use.
205    *
206    * @return static
207    *   The object itself for chaining.
208    */
209   public function setClass($class) {
210     $this->definition['class'] = $class;
211     return $this;
212   }
213
214   /**
215    * {@inheritdoc}
216    */
217   public function getSettings() {
218     return isset($this->definition['settings']) ? $this->definition['settings'] : [];
219   }
220
221   /**
222    * Sets the array of settings, as required by the used class.
223    *
224    * @param array $settings
225    *   The array of settings.
226    *
227    * @return static
228    *   The object itself for chaining.
229    */
230   public function setSettings(array $settings) {
231     $this->definition['settings'] = $settings;
232     return $this;
233   }
234
235   /**
236    * {@inheritdoc}
237    */
238   public function getSetting($setting_name) {
239     return isset($this->definition['settings'][$setting_name]) ? $this->definition['settings'][$setting_name] : NULL;
240   }
241
242   /**
243    * Sets a definition setting.
244    *
245    * @param string $setting_name
246    *   The definition setting to set.
247    * @param mixed $value
248    *   The value to set.
249    *
250    * @return static
251    *   The object itself for chaining.
252    */
253   public function setSetting($setting_name, $value) {
254     $this->definition['settings'][$setting_name] = $value;
255     return $this;
256   }
257
258   /**
259    * {@inheritdoc}
260    */
261   public function getConstraints() {
262     $constraints = isset($this->definition['constraints']) ? $this->definition['constraints'] : [];
263     $constraints += $this->getTypedDataManager()->getDefaultConstraints($this);
264     return $constraints;
265   }
266
267   /**
268    * {@inheritdoc}
269    */
270   public function getConstraint($constraint_name) {
271     $constraints = $this->getConstraints();
272     return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
273   }
274
275   /**
276    * {@inheritdoc}
277    */
278   public function setConstraints(array $constraints) {
279     $this->definition['constraints'] = $constraints;
280     return $this;
281   }
282
283   /**
284    * {@inheritdoc}
285    */
286   public function addConstraint($constraint_name, $options = NULL) {
287     $this->definition['constraints'][$constraint_name] = $options;
288     return $this;
289   }
290
291   /**
292    * {@inheritdoc}
293    *
294    * This is for BC support only.
295    * @todo: Remove in https://www.drupal.org/node/1928868.
296    */
297   public function offsetExists($offset) {
298     // PHP's array access does not work correctly with isset(), so we have to
299     // bake isset() in here. See https://bugs.php.net/bug.php?id=41727.
300     return array_key_exists($offset, $this->definition) && isset($this->definition[$offset]);
301   }
302
303   /**
304    * {@inheritdoc}
305    *
306    * This is for BC support only.
307    * @todo: Remove in https://www.drupal.org/node/1928868.
308    */
309   public function &offsetGet($offset) {
310     if (!isset($this->definition[$offset])) {
311       $this->definition[$offset] = NULL;
312     }
313     return $this->definition[$offset];
314   }
315
316   /**
317    * {@inheritdoc}
318    *
319    * This is for BC support only.
320    * @todo: Remove in https://www.drupal.org/node/1928868.
321    */
322   public function offsetSet($offset, $value) {
323     $this->definition[$offset] = $value;
324   }
325
326   /**
327    * {@inheritdoc}
328    *
329    * This is for BC support only.
330    * @todo: Remove in https://www.drupal.org/node/1928868.
331    */
332   public function offsetUnset($offset) {
333     unset($this->definition[$offset]);
334   }
335
336   /**
337    * Returns all definition values as array.
338    *
339    * @return array
340    */
341   public function toArray() {
342     return $this->definition;
343   }
344
345   /**
346    * {@inheritdoc}
347    */
348   public function __sleep() {
349     // Never serialize the typed data manager.
350     $vars = get_object_vars($this);
351     unset($vars['typedDataManager']);
352     return array_keys($vars);
353   }
354
355   /**
356    * {@inheritdoc}
357    */
358   public function isInternal() {
359     // Respect the definition, otherwise default to TRUE for computed fields.
360     if (isset($this->definition['internal'])) {
361       return $this->definition['internal'];
362     }
363     return $this->isComputed();
364   }
365
366   /**
367    * Sets the whether the data value should be internal.
368    *
369    * @param bool $internal
370    *   Whether the data value should be internal.
371    *
372    * @return $this
373    */
374   public function setInternal($internal) {
375     $this->definition['internal'] = $internal;
376     return $this;
377   }
378
379 }