Completed
Push — SOLID ( c3623c...92d068 )
by Wouter
02:30
created

BlockquoteConverter::getCiteHtml()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

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