Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / TypedData / TypedData.php
1 <?php
2
3 namespace Drupal\Core\TypedData;
4
5 use Drupal\Component\Plugin\PluginInspectionInterface;
6 use Drupal\Core\StringTranslation\StringTranslationTrait;
7
8 /**
9  * The abstract base class for typed data.
10  *
11  * Classes deriving from this base class have to declare $value
12  * or override getValue() or setValue().
13  *
14  * @ingroup typed_data
15  */
16 abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
17
18   use StringTranslationTrait;
19   use TypedDataTrait;
20
21   /**
22    * The data definition.
23    *
24    * @var \Drupal\Core\TypedData\DataDefinitionInterface
25    */
26   protected $definition;
27
28   /**
29    * The property name.
30    *
31    * @var string
32    */
33   protected $name;
34
35   /**
36    * The parent typed data object.
37    *
38    * @var \Drupal\Core\TypedData\TraversableTypedDataInterface|null
39    */
40   protected $parent;
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) {
46     return new static($definition, $name, $parent);
47   }
48
49   /**
50    * Constructs a TypedData object given its definition and context.
51    *
52    * @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
53    *   The data definition.
54    * @param string $name
55    *   (optional) The name of the created property, or NULL if it is the root
56    *   of a typed data tree. Defaults to NULL.
57    * @param \Drupal\Core\TypedData\TypedDataInterface $parent
58    *   (optional) The parent object of the data property, or NULL if it is the
59    *   root of a typed data tree. Defaults to NULL.
60    *
61    * @see \Drupal\Core\TypedData\TypedDataManager::create()
62    */
63   public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
64     $this->definition = $definition;
65     $this->parent = $parent;
66     $this->name = $name;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getPluginId() {
73     return $this->definition['type'];
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   public function getPluginDefinition() {
80     return $this->getTypedDataManager()->getDefinition($this->definition->getDataType());
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function getDataDefinition() {
87     return $this->definition;
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function getValue() {
94     return $this->value;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function setValue($value, $notify = TRUE) {
101     $this->value = $value;
102     // Notify the parent of any changes.
103     if ($notify && isset($this->parent)) {
104       $this->parent->onChange($this->name);
105     }
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getString() {
112     return (string) $this->getValue();
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   public function getConstraints() {
119     $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
120     $constraints = [];
121     foreach ($this->definition->getConstraints() as $name => $options) {
122       $constraints[] = $constraint_manager->create($name, $options);
123     }
124     return $constraints;
125   }
126
127   /**
128    * {@inheritdoc}
129    */
130   public function validate() {
131     return $this->getTypedDataManager()->getValidator()->validate($this);
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function applyDefaultValue($notify = TRUE) {
138     // Default to no default value.
139     $this->setValue(NULL, $notify);
140     return $this;
141   }
142
143   /**
144    * {@inheritdoc}
145    */
146   public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) {
147     $this->parent = $parent;
148     $this->name = $name;
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   public function getName() {
155     return $this->name;
156   }
157
158   /**
159    * {@inheritdoc}
160    */
161   public function getRoot() {
162     if (isset($this->parent)) {
163       return $this->parent->getRoot();
164     }
165     // If no parent is set, this is the root of the data tree.
166     return $this;
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   public function getPropertyPath() {
173     if (isset($this->parent)) {
174       // The property path of this data object is the parent's path appended
175       // by this object's name.
176       $prefix = $this->parent->getPropertyPath();
177       return (strlen($prefix) ? $prefix . '.' : '') . $this->name;
178     }
179     // If no parent is set, this is the root of the data tree. Thus the property
180     // path equals the name of this data object.
181     elseif (isset($this->name)) {
182       return $this->name;
183     }
184     return '';
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   public function getParent() {
191     return $this->parent;
192   }
193
194   /**
195    * {@inheritdoc}
196    */
197   public function __sleep() {
198     $vars = get_object_vars($this);
199     // Prevent services from being serialized. static::getStringTranslation()
200     // and static::getTypedDataManager() lazy-load them after $this has been
201     // unserialized.
202     // @todo Replace this with
203     //   \Drupal\Core\DependencyInjection\DependencySerializationTrait before
204     //   Drupal 9.0.0. We cannot use that now, because child classes already use
205     //   it and PHP 5 would consider that conflicts.
206     unset($vars['stringTranslation']);
207     unset($vars['typedDataManager']);
208
209     return array_keys($vars);
210   }
211
212 }