Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\Core\TypedData\TypedData.
Dries Buytaert
committed
*/
namespace Drupal\Core\TypedData;
Dries Buytaert
committed
Alex Pott
committed
use Drupal\Component\Plugin\PluginInspectionInterface;
Dries Buytaert
committed
/**
* The abstract base class for typed data.
*
* Classes deriving from this base class have to declare $value
* or override getValue() or setValue().
*/
Alex Pott
committed
abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
Dries Buytaert
committed
/**
* The data definition.
*
* @var array
*/
protected $definition;
/**
catch
committed
* The property name.
*
* @var string
*/
protected $name;
/**
* The parent typed data object.
*
* @var \Drupal\Core\TypedData\TypedDataInterface
*/
protected $parent;
/**
* Constructs a TypedData object given its definition and context.
Dries Buytaert
committed
*
* @param array $definition
* The data definition.
catch
committed
* @param string $name
* (optional) The name of the created property, or NULL if it is the root
* of a typed data tree. Defaults to NULL.
* @param \Drupal\Core\TypedData\TypedDataInterface $parent
* (optional) The parent object of the data property, or NULL if it is the
* root of a typed data tree. Defaults to NULL.
Dries Buytaert
committed
*
* @see Drupal\Core\TypedData\TypedDataManager::create()
Dries Buytaert
committed
*/
catch
committed
public function __construct(array $definition, $name = NULL, TypedDataInterface $parent = NULL) {
Dries Buytaert
committed
$this->definition = $definition;
catch
committed
$this->parent = $parent;
$this->name = $name;
Dries Buytaert
committed
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getType().
Dries Buytaert
committed
*/
public function getType() {
return $this->definition['type'];
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function getPluginId() {
return $this->definition['type'];
}
/**
* {@inheritdoc}
*/
public function getPluginDefinition() {
return \Drupal::typedData()->getDefinition($this->definition['type']);
}
Dries Buytaert
committed
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getDefinition().
Dries Buytaert
committed
*/
public function getDefinition() {
return $this->definition;
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getValue().
Dries Buytaert
committed
*/
public function getValue() {
return $this->value;
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::setValue().
Dries Buytaert
committed
*/
catch
committed
public function setValue($value, $notify = TRUE) {
// Notify the parent of any changes to be made.
if ($notify && isset($this->parent)) {
$this->parent->onChange($this->name);
}
Dries Buytaert
committed
$this->value = $value;
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getString().
Dries Buytaert
committed
*/
public function getString() {
return (string) $this->getValue();
}
Dries Buytaert
committed
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getConstraints().
*/
public function getConstraints() {
// @todo: Add the typed data manager as proper dependency.
Alex Pott
committed
return \Drupal::typedData()->getConstraints($this->definition);
Dries Buytaert
committed
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::validate().
*/
public function validate() {
Dries Buytaert
committed
// @todo: Add the typed data manager as proper dependency.
Alex Pott
committed
return \Drupal::typedData()->getValidator()->validate($this);
}
catch
committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::setContext().
*/
public function setContext($name = NULL, TypedDataInterface $parent = NULL) {
$this->parent = $parent;
$this->name = $name;
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getName().
*/
public function getName() {
return $this->name;
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getRoot().
*/
public function getRoot() {
if (isset($this->parent)) {
return $this->parent->getRoot();
}
// If no parent is set, this is the root of the data tree.
return $this;
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getPropertyPath().
*/
public function getPropertyPath() {
if (isset($this->parent)) {
// The property path of this data object is the parent's path appended
// by this object's name.
$prefix = $this->parent->getPropertyPath();
return (strlen($prefix) ? $prefix . '.' : '') . $this->name;
}
// If no parent is set, this is the root of the data tree. Thus the property
// path equals the name of this data object.
elseif (isset($this->name)) {
return $this->name;
}
return '';
}
/**
* Implements \Drupal\Core\TypedData\TypedDataInterface::getParent().
*
* @return \Drupal\Core\Entity\Field\FieldInterface
*/
public function getParent() {
return $this->parent;
}