comparison core/lib/Drupal/Core/Template/AttributeValueBase.php @ 0:4c8ae668cc8c

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