63e4038abdfd309c8eae687017e772f969b3ccd5
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / Context / ContextDefinition.php
1 <?php
2
3 namespace Drupal\Core\Plugin\Context;
4
5 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6 use Drupal\Core\TypedData\TypedDataTrait;
7
8 /**
9  * Defines a class for context definitions.
10  */
11 class ContextDefinition implements ContextDefinitionInterface {
12
13   use DependencySerializationTrait;
14
15   use TypedDataTrait;
16
17   /**
18    * The data type of the data.
19    *
20    * @var string
21    *   The data type.
22    */
23   protected $dataType;
24
25   /**
26    * The human-readable label.
27    *
28    * @var string
29    *   The label.
30    */
31   protected $label;
32
33   /**
34    * The human-readable description.
35    *
36    * @var string|null
37    *   The description, or NULL if no description is available.
38    */
39   protected $description;
40
41   /**
42    * Whether the data is multi-valued, i.e. a list of data items.
43    *
44    * @var bool
45    */
46   protected $isMultiple = FALSE;
47
48   /**
49    * Determines whether a data value is required.
50    *
51    * @var bool
52    *   Whether a data value is required.
53    */
54   protected $isRequired = TRUE;
55
56   /**
57    * The default value.
58    *
59    * @var mixed
60    */
61   protected $defaultValue;
62
63   /**
64    * An array of constraints.
65    *
66    * @var array[]
67    */
68   protected $constraints = [];
69
70   /**
71    * Creates a new context definition.
72    *
73    * @param string $data_type
74    *   The data type for which to create the context definition. Defaults to
75    *   'any'.
76    *
77    * @return static
78    *   The created context definition object.
79    */
80   public static function create($data_type = 'any') {
81     return new static(
82       $data_type
83     );
84   }
85
86   /**
87    * Constructs a new context definition object.
88    *
89    * @param string $data_type
90    *   The required data type.
91    * @param string|null $label
92    *   The label of this context definition for the UI.
93    * @param bool $required
94    *   Whether the context definition is required.
95    * @param bool $multiple
96    *   Whether the context definition is multivalue.
97    * @param string|null $description
98    *   The description of this context definition for the UI.
99    * @param mixed $default_value
100    *   The default value of this definition.
101    */
102   public function __construct($data_type = 'any', $label = NULL, $required = TRUE, $multiple = FALSE, $description = NULL, $default_value = NULL) {
103     $this->dataType = $data_type;
104     $this->label = $label;
105     $this->isRequired = $required;
106     $this->isMultiple = $multiple;
107     $this->description = $description;
108     $this->defaultValue = $default_value;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function getDataType() {
115     return $this->dataType;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function setDataType($data_type) {
122     $this->dataType = $data_type;
123     return $this;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function getLabel() {
130     return $this->label;
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   public function setLabel($label) {
137     $this->label = $label;
138     return $this;
139   }
140
141   /**
142    * {@inheritdoc}
143    */
144   public function getDescription() {
145     return $this->description;
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function setDescription($description) {
152     $this->description = $description;
153     return $this;
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function isMultiple() {
160     return $this->isMultiple;
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function setMultiple($multiple = TRUE) {
167     $this->isMultiple = $multiple;
168     return $this;
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function isRequired() {
175     return $this->isRequired;
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public function setRequired($required = TRUE) {
182     $this->isRequired = $required;
183     return $this;
184   }
185
186   /**
187    * {@inheritdoc}
188    */
189   public function getDefaultValue() {
190     return $this->defaultValue;
191   }
192
193   /**
194    * {@inheritdoc}
195    */
196   public function setDefaultValue($default_value) {
197     $this->defaultValue = $default_value;
198     return $this;
199   }
200
201   /**
202    * {@inheritdoc}
203    */
204   public function getConstraints() {
205     // @todo Apply defaults.
206     return $this->constraints;
207   }
208
209   /**
210    * {@inheritdoc}
211    */
212   public function getConstraint($constraint_name) {
213     $constraints = $this->getConstraints();
214     return isset($constraints[$constraint_name]) ? $constraints[$constraint_name] : NULL;
215   }
216
217   /**
218    * {@inheritdoc}
219    */
220   public function setConstraints(array $constraints) {
221     $this->constraints = $constraints;
222     return $this;
223   }
224
225   /**
226    * {@inheritdoc}
227    */
228   public function addConstraint($constraint_name, $options = NULL) {
229     $this->constraints[$constraint_name] = $options;
230     return $this;
231   }
232
233   /**
234    * {@inheritdoc}
235    */
236   public function getDataDefinition() {
237     if ($this->isMultiple()) {
238       $definition = $this->getTypedDataManager()->createListDataDefinition($this->getDataType());
239     }
240     else {
241       $definition = $this->getTypedDataManager()->createDataDefinition($this->getDataType());
242     }
243
244     if (!$definition) {
245       throw new \Exception("The data type '{$this->getDataType()}' is invalid");
246     }
247     $definition->setLabel($this->getLabel())
248       ->setDescription($this->getDescription())
249       ->setRequired($this->isRequired());
250     $constraints = $definition->getConstraints() + $this->getConstraints();
251     $definition->setConstraints($constraints);
252     return $definition;
253   }
254
255 }