annotate core/lib/Drupal/Core/Template/AttributeValueBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Template;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Html;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Defines the base class for an attribute type.
Chris@0 9 *
Chris@0 10 * @see \Drupal\Core\Template\Attribute
Chris@0 11 */
Chris@0 12 abstract class AttributeValueBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Renders '$name=""' if $value is an empty string.
Chris@0 16 *
Chris@0 17 * @see \Drupal\Core\Template\AttributeValueBase::render()
Chris@0 18 */
Chris@0 19 const RENDER_EMPTY_ATTRIBUTE = TRUE;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The value itself.
Chris@0 23 *
Chris@0 24 * @var mixed
Chris@0 25 */
Chris@0 26 protected $value;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The name of the value.
Chris@0 30 *
Chris@0 31 * @var mixed
Chris@0 32 */
Chris@0 33 protected $name;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Constructs a \Drupal\Core\Template\AttributeValueBase object.
Chris@0 37 */
Chris@0 38 public function __construct($name, $value) {
Chris@0 39 $this->name = $name;
Chris@0 40 $this->value = $value;
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Returns a string representation of the attribute.
Chris@0 45 *
Chris@0 46 * While __toString only returns the value in a string form, render()
Chris@0 47 * contains the name of the attribute as well.
Chris@0 48 *
Chris@0 49 * @return string
Chris@0 50 * The string representation of the attribute.
Chris@0 51 */
Chris@0 52 public function render() {
Chris@0 53 $value = (string) $this;
Chris@0 54 if (isset($this->value) && static::RENDER_EMPTY_ATTRIBUTE || !empty($value)) {
Chris@0 55 return Html::escape($this->name) . '="' . $value . '"';
Chris@0 56 }
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * Returns the raw value.
Chris@0 61 */
Chris@0 62 public function value() {
Chris@0 63 return $this->value;
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Implements the magic __toString() method.
Chris@0 68 */
Chris@0 69 abstract public function __toString();
Chris@0 70
Chris@0 71 }