Link::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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