Passed
Push — master ( b04009...b8980d )
by Tobias
02:33
created

HtmlConversion::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace WrkLst\DocxMustache;
4
5
use Exception;
6
use Illuminate\Support\Facades\Log;
7
8
class HtmlConversion
9
{
10
    public function __construct($value)
11
    {
12
        return $this->convertHtmlToOpenXML($value);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
13
    }
14
15
    /**
16
     * @param string $value
17
     */
18
    protected function convertHtmlToOpenXML($value)
19
    {
20
        $line_breaks = array("&lt;br /&gt;", "&lt;br/&gt;", "&lt;br&gt;", "<br />", "<br/>", "<br>");
21
        $value = str_replace($line_breaks, '<w:br/>', $value);
22
23
        $value = $this->convertHtmlToOpenXMLTag($value, "b");
24
        $value = $this->convertHtmlToOpenXMLTag($value, "i");
25
        $value = $this->convertHtmlToOpenXMLTag($value, "u");
26
27
        return $value;
28
    }
29
30
    protected function convertHtmlToOpenXMLTag($value, $tag = "b")
31
    {
32
        $value_array = array();
33
        $run_again = false;
34
        //this could be used instead if html was already escaped
35
        /*
36
        $bo = "&lt;";
37
        $bc = "&gt;";
38
        */
39
        $bo = "<";
40
        $bc = ">";
41
42
        //get first BOLD
43
        $tag_open_values = explode($bo.$tag.$bc, $value, 2);
44
45
        if (count($tag_open_values) > 1)
46
        {
47
            //save everything before the bold and close it
48
            $value_array[] = $tag_open_values[0];
49
            $value_array[] = '</w:t></w:r>';
50
51
            //define styling parameters
52
            $wrPr_open = strrpos($tag_open_values[0], '<w:rPr>');
53
            $wrPr_close = strrpos($tag_open_values[0], '</w:rPr>', $wrPr_open);
54
            $neutral_style = '<w:r><w:rPr>'.substr($tag_open_values[0], ($wrPr_open + 7), ($wrPr_close - ($wrPr_open + 7))).'</w:rPr><w:t>';
55
            $tagged_style = '<w:r><w:rPr><w:'.$tag.'/>'.substr($tag_open_values[0], ($wrPr_open + 7), ($wrPr_close - ($wrPr_open + 7))).'</w:rPr><w:t>';
56
57
            //open new text run and make it bold, include previous styling
58
            $value_array[] = $tagged_style;
59
            //get everything before bold close and after
60
            $tag_close_values = explode($bo.'/'.$tag.$bc, $tag_open_values[1], 2);
61
            //add bold text
62
            $value_array[] = $tag_close_values[0];
63
            //close bold run
64
            $value_array[] = '</w:t></w:r>';
65
            //open run for after bold
66
            $value_array[] = $neutral_style;
67
            $value_array[] = $tag_close_values[1];
68
69
            $run_again = true;
70
        } else {
71
            $value_array[] = $tag_open_values[0];
72
        }
73
74
        $value = implode('', $value_array);
75
76
        if ($run_again) {
77
                    $value = $this->convertHtmlToOpenXMLTag($value, $tag);
78
        }
79
80
        return $value;
81
    }
82
}
83