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

BlockquoteConverter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 5
c 3
b 0
f 0
lcom 1
cbo 1
dl 0
loc 50
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toJson() 0 16 1
A matches() 0 4 1
A getCiteHtml() 0 13 2
A getCiteNodes() 0 9 1
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