Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\Core\TypedData;
Dries Buytaert
committed
Alex Pott
committed
use Drupal\Component\Plugin\PluginInspectionInterface;
catch
committed
use Drupal\Core\StringTranslation\StringTranslationTrait;
Alex Pott
committed
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().
Angie Byron
committed
*
* @ingroup typed_data
Dries Buytaert
committed
*/
Alex Pott
committed
abstract class TypedData implements TypedDataInterface, PluginInspectionInterface {
Dries Buytaert
committed
catch
committed
use StringTranslationTrait;
Alex Bronstein
committed
use TypedDataTrait;
catch
committed
Dries Buytaert
committed
/**
* The data definition.
*
Angie Byron
committed
* @var \Drupal\Core\TypedData\DataDefinitionInterface
Dries Buytaert
committed
*/
protected $definition;
/**
catch
committed
* The property name.
*
* @var string
*/
protected $name;
/**
* The parent typed data object.
*
* @var \Drupal\Core\TypedData\TraversableTypedDataInterface|null
catch
committed
*/
protected $parent;
Angie Byron
committed
/**
* {@inheritdoc}
*/
public static function createInstance($definition, $name = NULL, TraversableTypedDataInterface $parent = NULL) {
Angie Byron
committed
return new static($definition, $name, $parent);
}
catch
committed
/**
* Constructs a TypedData object given its definition and context.
Dries Buytaert
committed
*
Angie Byron
committed
* @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
Dries Buytaert
committed
* 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
*
Alex Pott
committed
* @see \Drupal\Core\TypedData\TypedDataManager::create()
Dries Buytaert
committed
*/
public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
Dries Buytaert
committed
$this->definition = $definition;
catch
committed
$this->parent = $parent;
$this->name = $name;
Dries Buytaert
committed
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function getPluginId() {
return $this->definition['type'];
}
/**
* {@inheritdoc}
*/
public function getPluginDefinition() {
Alex Bronstein
committed
return $this->getTypedDataManager()->getDefinition($this->definition->getDataType());
Alex Pott
committed
}
Dries Buytaert
committed
/**
Angie Byron
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getDataDefinition() {
Dries Buytaert
committed
return $this->definition;
}
/**
Angie Byron
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getValue() {
return $this->value;
}
/**
Angie Byron
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
catch
committed
public function setValue($value, $notify = TRUE) {
$this->value = $value;
// Notify the parent of any changes.
catch
committed
if ($notify && isset($this->parent)) {
$this->parent->onChange($this->name);
}
Dries Buytaert
committed
}
/**
Angie Byron
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getString() {
return (string) $this->getValue();
}
Dries Buytaert
committed
/**
Angie Byron
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function getConstraints() {
Alex Bronstein
committed
$constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager();
$constraints = [];
foreach ($this->definition->getConstraints() as $name => $options) {
$constraints[] = $constraint_manager->create($name, $options);
}
return $constraints;
Dries Buytaert
committed
}
/**
Angie Byron
committed
* {@inheritdoc}
*/
public function validate() {
Alex Bronstein
committed
return $this->getTypedDataManager()->getValidator()->validate($this);
}
catch
committed
/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
// Default to no default value.
$this->setValue(NULL, $notify);
return $this;
}
catch
committed
/**
Angie Byron
committed
* {@inheritdoc}
catch
committed
*/
public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) {
catch
committed
$this->parent = $parent;
$this->name = $name;
}
/**
Angie Byron
committed
* {@inheritdoc}
catch
committed
*/
public function getName() {
return $this->name;
}
/**
Angie Byron
committed
* {@inheritdoc}
catch
committed
*/
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;
}
/**
Angie Byron
committed
* {@inheritdoc}
catch
committed
*/
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 '';
}
/**
Angie Byron
committed
* {@inheritdoc}
catch
committed
*/
public function getParent() {
return $this->parent;
}
/**
* {@inheritdoc}
*/
public function __sleep() {
$vars = get_object_vars($this);
// Prevent services from being serialized. static::getStringTranslation()
// and static::getTypedDataManager() lazy-load them after $this has been
// unserialized.
// @todo Replace this with
// \Drupal\Core\DependencyInjection\DependencySerializationTrait before
// Drupal 9.0.0. We cannot use that now, because child classes already use
// it and PHP 5 would consider that conflicts.
unset($vars['stringTranslation']);
unset($vars['typedDataManager']);
return array_keys($vars);
}