Completed
Push — SOLID ( ba508c...d6070b )
by Wouter
01:49
created

BlockquoteConverter::toJson()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 8.8571
cc 3
eloc 15
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Sioen\HtmlToJson;
4
5
class BlockquoteConverter extends Converter
6
{
7 1
    public function toJson(\DOMElement $node)
8
    {
9
        // check if the quote contains a cite
10 1
        $cite = '';
11
12 1
        foreach ($node->childNodes as $child) {
13
            // if it contains a 'cite' node, we should add it in the cite property
14 1
            if ($child->nodeName == 'cite') {
15 1
                $html = $child->ownerDocument->saveXML($child);
16 1
                $html = preg_replace('/<(\/|)cite>/i', '', $html);
17 1
                $child->parentNode->removeChild($child);
18 1
                $cite = ' ' . $this->htmlToMarkdown($html);
19 1
            }
20 1
        }
21
22
        // we use the remaining html to create the remaining text
23 1
        $html = $node->ownerDocument->saveXML($node);
24 1
        $html = preg_replace('/<(\/|)blockquote>/i', '', $html);
25
26
        return array(
27 1
            'type' => 'quote',
28
            'data' => array(
29 1
                'text' => ' ' . $this->htmlToMarkdown($html),
30
                'cite' => $cite
31 1
            )
32 1
        );
33
    }
34
35 1
    public function matches(\DomElement $node)
36
    {
37 1
        return $node->nodeName === 'blockquote';
38
    }
39
}
40