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