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