BlockquoteConverter::getCiteNodes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Sioen\HtmlToJson;
4
5
use Sioen\SirTrevorBlock;
6
7
final class BlockquoteConverter implements Converter
8
{
9
    use HtmlToMarkdown;
10
11 1
    public function toJson(\DOMElement $node)
12
    {
13 1
        $cite = $this->getCiteHtml($node);
14
15
        // we use the remaining html to create the remaining text
16 1
        $html = $node->ownerDocument->saveXML($node);
17 1
        $html = preg_replace('/<(\/|)blockquote>/i', '', $html);
18
19 1
        return new SirTrevorBlock(
20 1
            'quote',
21
            array(
22 1
                'text' => ' ' . $this->htmlToMarkdown($html),
23 1
                'cite' => $cite,
24
            )
25 1
        );
26
    }
27
28 1
    public function matches(\DomElement $node)
29
    {
30 1
        return $node->nodeName === 'blockquote';
31
    }
32
33 1
    private function getCiteHtml(\DOMElement $node)
34
    {
35 1
        $cite = '';
36
37 1
        foreach ($this->getCiteNodes($node) as $child) {
38 1
            $html = $child->ownerDocument->saveXML($child);
39 1
            $html = preg_replace('/<(\/|)cite>/i', '', $html);
40 1
            $child->parentNode->removeChild($child);
41 1
            $cite = ' ' . $this->htmlToMarkdown($html);
42 1
        }
43
44 1
        return $cite;
45
    }
46
47 1
    private function getCiteNodes(\DOMElement $node)
48
    {
49 1
        return array_filter(
50 1
            iterator_to_array($node->childNodes),
51 1
            function (\DOMElement $childNode) {
52 1
                return $childNode->nodeName == 'cite';
53
            }
54 1
        );
55
    }
56
}
57