Test Setup Failed
Pull Request — latest (#3)
by Mark
31:07
created

Node::setDocument()   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 1
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
            $class .= ' ' . $value;
60
        }
61
62
        if ($class) {
63
            $this->setAttribute('class', $class);
64
        }
65
    }
66
67
    /**
68
     * @param mixed $default
69
     *
70
     * @return mixed
71
     */
72
    public function getAttribute(string $name, $default = null)
73
    {
74
        return $this->getAttributes()->get($name, $default);
75
    }
76
77
    public function getAttributes(): Data
78
    {
79
        /** @var Data $attributes */
80
        $attributes = $this->get('attributes');
81
82
        return $attributes;
83
    }
84
85
    public function getContent(): string
86
    {
87
        return $this->content;
88
    }
89
90
    public function getDocument(): ?Document
91
    {
92
        return $this->document;
93
    }
94
95
    /**
96
     * @param mixed $value
97
     */
98
    public function setAttribute(string $name, $value): void
99
    {
100
        $this->getAttributes()->set($name, $value);
101
    }
102
103
    /**
104
     * @param array<string, mixed> $attributes
105
     */
106
    public function setAttributes(array $attributes = []): void
107
    {
108
        $this->set('attributes', new Data($attributes));
109
    }
110
111
    public function setContent(string $contents): void
112
    {
113
        $this->content = $contents;
114
    }
115
116
    public function setDocument(?Document $document = null): void
117
    {
118
        $this->document = $document;
119
    }
120
121
    public function replaceWith(Node $replacement): void
122
    {
123
        if (! $this->document) {
124
            return;
125
        }
126
127
        $this->document->replaceNode($this, $replacement);
128
        $this->document = null;
129
    }
130
}
131