forked from Teein/Html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttribute.php
More file actions
75 lines (64 loc) · 1.8 KB
/
Attribute.php
File metadata and controls
75 lines (64 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Teein\Html\Ast;
use Teein\Html\VirtualDom\Attribute as AttributeInterface;
/**
* An Attribute represents a html-attribute, that is a name-value-pair that is
* assigned to an Element.
*/
final class Attribute implements AttributeInterface
{
protected $name;
protected $value;
/**
* Construct a new Attribute with name set to $name and value set to $value
* @param string $name The name of the new Attribute
* @param string $value The value of the new Attribute
*/
public function __construct(string $name, string $value)
{
$this->name = $name;
$this->value = $value;
}
/**
* Get the html-representation of this Attribute
*/
public function toHtml() : string
{
$htmlName = htmlspecialchars($this->name, ENT_QUOTES | ENT_HTML5);
$htmlValue = htmlspecialchars($this->value, ENT_QUOTES | ENT_HTML5);
return "$htmlName=\"$htmlValue\"";
}
/**
* Get the name of this Attribute
*/
public function getName() : string
{
return $this->name;
}
/**
* Get the value of this Attribute
*/
public function getValue() : string
{
return $this->value;
}
/**
* Get a new Attribute that is like this one but with name set to $name
*
* @param string $name The name of the new Attribute
*/
public function setName(string $name) : AttributeInterface
{
return new Attribute($name, $this->value);
}
/**
* Get a new Attribute that is like this one but with value set to $value
*
* @param string $value The value of the new Attribute
*/
public function setValue(string $value) : AttributeInterface
{
return new Attribute($this->name, $value);
}
}