Completed
Push — develop ( c4d291...65fed0 )
by Peter
02:27
created

NodeTrait::replaceWith()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 4 Features 1
Metric Value
c 6
b 4
f 1
dl 0
loc 15
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2016 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\Dom;
12
13
use WebinoDraw\Exception;
14
15
/**
16
 * Class NodeTrait
17
 */
18
trait NodeTrait
19
{
20
    /**
21
     * @var callable[]
22
     */
23
    private $onReplace = [];
24
25
    /**
26
     * @param callable $callback
27
     * @return $this
28
     */
29
    public function setOnReplace(callable $callback)
30
    {
31
        $this->onReplace[] = $callback;
32
        return $this;
33
    }
34
35
    /**
36
     * @return Document
37
     */
38
    public function getOwnerDocument()
39
    {
40
        /** @noinspection PhpUndefinedFieldInspection */
41
        return $this->ownerDocument;
0 ignored issues
show
Bug introduced by
The property ownerDocument does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
    }
43
44
    /**
45
     * @return Element
46
     */
47
    public function getParentNode()
48
    {
49
        /** @noinspection PhpUndefinedFieldInspection */
50
        return $this->parentNode;
0 ignored issues
show
Bug introduced by
The property parentNode does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
    }
52
53
    /**
54
     * @param string $html
55
     * @return NodeInterface
56
     * @throws Exception\LogicException
57
     */
58
    public function replaceWith($html)
59
    {
60
        $frag = $this->getOwnerDocument()->createDocumentFragment();
61
        $frag->appendXml($html);
62
63
        $newNode = $this->getParentNode()->insertBefore($frag, $this);
64
65
        if (!empty($this->onReplace)) {
66
            foreach ($this->onReplace as $onReplace) {
67
                call_user_func($onReplace, $newNode, $this, $frag);
68
            }
69
        }
70
71
        return $newNode;
72
    }
73
}
74