definition = $definition; $this->parent = $parent; $this->name = $name; } /** * {@inheritdoc} */ public function getPluginId() { return $this->definition['type']; } /** * {@inheritdoc} */ public function getPluginDefinition() { return $this->getTypedDataManager()->getDefinition($this->definition->getDataType()); } /** * {@inheritdoc} */ public function getDataDefinition() { return $this->definition; } /** * {@inheritdoc} */ public function getValue() { return $this->value; } /** * {@inheritdoc} */ public function setValue($value, $notify = TRUE) { $this->value = $value; // Notify the parent of any changes. if ($notify && isset($this->parent)) { $this->parent->onChange($this->name); } } /** * {@inheritdoc} */ public function getString() { return (string) $this->getValue(); } /** * {@inheritdoc} */ public function getConstraints() { $constraint_manager = $this->getTypedDataManager()->getValidationConstraintManager(); $constraints = []; foreach ($this->definition->getConstraints() as $name => $options) { $constraints[] = $constraint_manager->create($name, $options); } return $constraints; } /** * {@inheritdoc} */ public function validate() { return $this->getTypedDataManager()->getValidator()->validate($this); } /** * {@inheritdoc} */ public function applyDefaultValue($notify = TRUE) { // Default to no default value. $this->setValue(NULL, $notify); return $this; } /** * {@inheritdoc} */ public function setContext($name = NULL, TraversableTypedDataInterface $parent = NULL) { $this->parent = $parent; $this->name = $name; } /** * {@inheritdoc} */ public function getName() { return $this->name; } /** * {@inheritdoc} */ 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; } /** * {@inheritdoc} */ 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(); // Variables in double quotes used to leverage fast string concatenation. // In PHP 7+ concatenation with variable inside string is the fastest. // @see https://blog.blackfire.io/php-7-performance-improvements-encapsed-strings-optimization.html // This is being done because the code can run in the critical path. return $prefix !== '' ? "{$prefix}.{$this->name}" : $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 ''; } /** * {@inheritdoc} */ public function getParent() { return $this->parent; } }