23b2f098784f2007e7d9e231bdd0b3d1a52da640
[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    * Sets an array of validation constraints.
277    *
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.
282    *
283    * @return $this
284    */
285   public function setConstraints(array $constraints) {
286     $this->definition['constraints'] = $constraints;
287     return $this;
288   }
289
290   /**
291    * {@inheritdoc}
292    */
293   public function addConstraint($constraint_name, $options = NULL) {
294     $this->definition['constraints'][$constraint_name] = $options;
295     return $this;
296   }
297
298   /**
299    * {@inheritdoc}
300    *
301    * This is for BC support only.
302    * @todo: Remove in https://www.drupal.org/node/1928868.
303    */
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]);
308   }
309
310   /**
311    * {@inheritdoc}
312    *
313    * This is for BC support only.
314    * @todo: Remove in https://www.drupal.org/node/1928868.
315    */
316   public function &offsetGet($offset) {
317     if (!isset($this->definition[$offset])) {
318       $this->definition[$offset] = NULL;
319     }
320     return $this->definition[$offset];
321   }
322
323   /**
324    * {@inheritdoc}
325    *
326    * This is for BC support only.
327    * @todo: Remove in https://www.drupal.org/node/1928868.
328    */
329   public function offsetSet($offset, $value) {
330     $this->definition[$offset] = $value;
331   }
332
333   /**
334    * {@inheritdoc}
335    *
336    * This is for BC support only.
337    * @todo: Remove in https://www.drupal.org/node/1928868.
338    */
339   public function offsetUnset($offset) {
340     unset($this->definition[$offset]);
341   }
342
343   /**
344    * Returns all definition values as array.
345    *
346    * @return array
347    */
348   public function toArray() {
349     return $this->definition;
350   }
351
352   /**
353    * {@inheritdoc}
354    */
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);
360   }
361
362   /**
363    * {@inheritdoc}
364    */
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'];
369     }
370     return $this->isComputed();
371   }
372
373   /**
374    * Sets the whether the data value should be internal.
375    *
376    * @param bool $internal
377    *   Whether the data value should be internal.
378    *
379    * @return $this
380    */
381   public function setInternal($internal) {
382     $this->definition['internal'] = $internal;
383     return $this;
384   }
385
386 }