Anchor::withAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
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\Set\ElementSet;
9
use RoyallTheFourth\HtmlDocument\Tag\Standard;
10
11
/**
12
 * Class Anchor
13
 * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a
14
 */
15
final class Anchor extends AbstractElement implements ParentElementInterface
16
{
17
    public function __construct(AttributeSet $attributes = null, ElementSet $children = null)
18
    {
19
        $this->attributes = $attributes ?? new AttributeSet();
20
        $this->children = $children ?? new ElementSet();
21
        $this->tag = new Standard('a', $attributes, $children);
22
    }
23
24
    public function withAttribute(string $name, string $value = null): Anchor
25
    {
26
        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...
27
            $attribute = new StandardAttribute($name, $value);
28
        } else {
29
            $attribute = new BooleanAttribute($name);
30
        }
31
32
        return new Anchor($this->attributes->add($attribute), $this->children);
33
    }
34
35
    public function withChild(ElementInterface $element): Anchor
36
    {
37
        return new Anchor($this->attributes, $this->children->add($element));
38
    }
39
40
    public function withHref($href): Anchor
41
    {
42
        return $this->withAttribute('href', $href);
43
    }
44
45
    public function withDownload($filename = null): Anchor
46
    {
47
        return $this->withAttribute('download', $filename);
48
    }
49
50
    public function withHrefLang($lang): Anchor
51
    {
52
        return $this->withAttribute('hreflang', $lang);
53
    }
54
55
    public function withPing($url): Anchor
56
    {
57
        return $this->withAttribute('ping', $url);
58
    }
59
60
    public function withRel($rel): Anchor
61
    {
62
        return $this->withAttribute('rel', $rel);
63
    }
64
65
    public function withTarget($target): Anchor
66
    {
67
        return $this->withAttribute('target', $target);
68
    }
69
70
    public function withType($type): Anchor
71
    {
72
        return $this->withAttribute('type', $type);
73
    }
74
}
75