1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RoyallTheFourth\HtmlDocument\Element; |
4
|
|
|
|
5
|
|
|
use RoyallTheFourth\HtmlDocument\Attribute\BooleanAttribute; |
6
|
|
|
use RoyallTheFourth\HtmlDocument\Attribute\StandardAttribute; |
7
|
|
|
use RoyallTheFourth\HtmlDocument\Set\AttributeSet; |
8
|
|
|
use RoyallTheFourth\HtmlDocument\Tag\EmptyTag; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Link |
12
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link |
13
|
|
|
*/ |
14
|
|
|
final class Link extends AbstractElement |
15
|
|
|
{ |
16
|
|
|
public function __construct(AttributeSet $attributes = null) |
17
|
|
|
{ |
18
|
|
|
$this->attributes = $attributes ?? new AttributeSet(); |
19
|
|
|
$this->tag = new EmptyTag('link', $attributes); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function withAttribute(string $name, string $value = null): Link |
23
|
|
|
{ |
24
|
|
|
if ($value) { |
|
|
|
|
25
|
|
|
$attribute = new StandardAttribute($name, $value); |
26
|
|
|
} else { |
27
|
|
|
$attribute = new BooleanAttribute($name); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return new Link($this->attributes->add($attribute)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function withCrossOrigin(string $policy): Link |
34
|
|
|
{ |
35
|
|
|
return $this->withAttribute('crossorigin', $policy); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function withHref(string $url): Link |
39
|
|
|
{ |
40
|
|
|
return $this->withAttribute('href', $url); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function withHrefLang(string $language): Link |
44
|
|
|
{ |
45
|
|
|
return $this->withAttribute('hreflang', $language); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $mediaQuery |
50
|
|
|
* @return Link |
51
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries |
52
|
|
|
*/ |
53
|
|
|
public function withMedia(string $mediaQuery): Link |
54
|
|
|
{ |
55
|
|
|
return $this->withAttribute('media', $mediaQuery); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $linkType |
60
|
|
|
* @return Link |
61
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types |
62
|
|
|
*/ |
63
|
|
|
public function withRel(string $linkType): Link |
64
|
|
|
{ |
65
|
|
|
return $this->withAttribute('rel', $linkType); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function withSizes(string $sizes = 'any'): Link |
69
|
|
|
{ |
70
|
|
|
return $this->withAttribute('sizes', $sizes); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function withType(string $mime): Link |
74
|
|
|
{ |
75
|
|
|
return $this->withAttribute('type', $mime); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: