Test Setup Failed
Pull Request — latest (#3)
by Mark
34:19
created

Node::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file was originally part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace UnicornFail\Emoji\Node;
18
19
use Dflydev\DotAccessData\Data;
20
21
abstract class Node extends Data implements \Stringable
22
{
23
    /**
24
     * @var Document|null
25
     *
26
     * @psalm-readonly-allow-private-mutation
27
     */
28
    protected $document;
29
30
    /** @var string */
31
    protected $content = '';
32
33
    /**
34
     * @param array<string, mixed> $data
35
     */
36
    public function __construct(string $content = '', array $data = [])
37
    {
38
        parent::__construct(['attributes' => new Data()]);
39
        $this->content = $content;
40
41
        $this->import($data);
42
    }
43
44
    public function __clone()
45
    {
46
        $this->document = null;
47
    }
48
49
    public function __toString(): string
50
    {
51
        return $this->getContent();
52
    }
53
54
    public function addClass(string ...$classes): void
55
    {
56
        $class = (string) $this->getAttribute('class', '');
57
58
        foreach ($classes as $value) {
59
            if ($class !== '') {
60
                $class .= ' ';
61
            }
62
63
            $class .= $value;
64
        }
65
66
        if ($class) {
67
            $this->setAttribute('class', $class);
68
        }
69
    }
70
71
    /**
72
     * @param mixed $default
73
     *
74
     * @return mixed
75
     */
76
    public function getAttribute(string $name, $default = null)
77
    {
78
        return $this->getAttributes()->get($name, $default);
79
    }
80
81
    public function getAttributes(): Data
82
    {
83
        /** @var Data $attributes */
84
        $attributes = $this->get('attributes');
85
86
        return $attributes;
87
    }
88
89
    public function getContent(): string
90
    {
91
        return $this->content;
92
    }
93
94
    public function getDocument(): ?Document
95
    {
96
        return $this->document;
97
    }
98
99
    public function hasAttribute(string $name): bool
100
    {
101
        return $this->getAttributes()->has($name);
102
    }
103
104
    /**
105
     * @param mixed $value
106
     */
107
    public function setAttribute(string $name, $value): void
108
    {
109
        $this->getAttributes()->set($name, $value);
110
    }
111
112
    /**
113
     * @param array<string, mixed> $attributes
114
     */
115
    public function setAttributes(array $attributes = []): void
116
    {
117
        $this->set('attributes', new Data($attributes));
118
    }
119
120
    public function setContent(string $contents): void
121
    {
122
        $this->content = $contents;
123
    }
124
125
    public function setDocument(?Document $document = null): void
126
    {
127
        $this->document = $document;
128
    }
129
130
    public function replaceWith(Node $replacement): void
131
    {
132
        if ($this->document) {
133
            $this->document->replaceNode($this, $replacement);
134
        }
135
    }
136
}
137