Completed
Pull Request — master (#22)
by Manuel
04:59
created

HtmlOutput::addFurtherInformationContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput;
4
5
use Sprain\SwissQrBill\PaymentPart\Output\AbstractOutput;
6
use Sprain\SwissQrBill\PaymentPart\Output\Element\OutputElementInterface;
7
use Sprain\SwissQrBill\PaymentPart\Output\Element\Placeholder;
8
use Sprain\SwissQrBill\PaymentPart\Output\Element\Text;
9
use Sprain\SwissQrBill\PaymentPart\Output\Element\Title;
10
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PlaceholderElementTemplate;
11
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PrintableStylesTemplate;
12
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\TextElementTemplate;
13
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PaymentPartTemplate;
14
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\TitleElementTemplate;
15
use Sprain\SwissQrBill\PaymentPart\Output\OutputInterface;
16
use Sprain\SwissQrBill\PaymentPart\Translation\Translation;
17
18
final class HtmlOutput extends AbstractOutput implements OutputInterface
19
{
20
    public function getPaymentPart(): string
21
    {
22
        $paymentPart = PaymentPartTemplate::TEMPLATE;
23
24
        $paymentPart = $this->addSwissQrCodeImage($paymentPart);
25
        $paymentPart = $this->addInformationContent($paymentPart);
26
        $paymentPart = $this->addInformationContentReceipt($paymentPart);
27
        $paymentPart = $this->addCurrencyContent($paymentPart);
28
        $paymentPart = $this->addAmountContent($paymentPart);
29
        $paymentPart = $this->addAmountContentReceipt($paymentPart);
30
        $paymentPart = $this->addFurtherInformationContent($paymentPart);
31
        $paymentPart = $this->addPrintableContent($paymentPart);
32
33
        $paymentPart = $this->translateContents($paymentPart, $this->getLanguage());
34
35
        return $paymentPart;
36
    }
37
38
    private function addSwissQrCodeImage(string $paymentPart): string
39
    {
40
        $qrCode = $this->qrBill->getQrCode();
41
        $qrCode->setWriterByExtension('svg');
42
43
        $paymentPart = str_replace('{{ swiss-qr-image }}', $qrCode->writeDataUri(), $paymentPart);
44
45
        return $paymentPart;
46
    }
47
48
    private function addInformationContent(string $paymentPart): string
49
    {
50
        $informationContent = '';
51
52
        foreach($this->getInformationElements() as $informationElement) {
53
            $informationContentPart = $this->getContentElement($informationElement);
54
            $informationContent .= $informationContentPart;
55
        }
56
57
        $paymentPart = str_replace('{{ information-content }}', $informationContent, $paymentPart);
58
59
        return $paymentPart;
60
    }
61
62
    private function addInformationContentReceipt(string $paymentPart): string
63
    {
64
        $informationContent = '';
65
66
        foreach($this->getInformationElementsOfReceipt() as $informationElement) {
67
            $informationContent .= $this->getContentElement($informationElement);
68
        }
69
70
        $paymentPart = str_replace('{{ information-content-receipt }}', $informationContent, $paymentPart);
71
72
        return $paymentPart;
73
    }
74
75
    private function addCurrencyContent(string $paymentPart): string
76
    {
77
        $currencyContent = '';
78
79
        foreach($this->getCurrencyElements() as $currencyElement) {
80
            $currencyContent .= $this->getContentElement($currencyElement);
81
        }
82
83
        $paymentPart = str_replace('{{ currency-content }}', $currencyContent, $paymentPart);
84
85
        return $paymentPart;
86
    }
87
88
    private function addAmountContent(string $paymentPart): string
89
    {
90
        $amountContent = '';
91
92
        foreach($this->getAmountElements() as $amountElement) {
93
            $amountContent .= $this->getContentElement($amountElement);
94
        }
95
96
        $paymentPart = str_replace('{{ amount-content }}', $amountContent, $paymentPart);
97
98
        return $paymentPart;
99
    }
100
101
    private function addAmountContentReceipt(string $paymentPart): string
102
    {
103
        $amountContent = '';
104
105
        foreach($this->getAmountElementsReceipt() as $amountElement) {
106
            $amountContent .= $this->getContentElement($amountElement);
107
        }
108
109
        $paymentPart = str_replace('{{ amount-content-receipt }}', $amountContent, $paymentPart);
110
111
        return $paymentPart;
112
    }
113
114
    private function addFurtherInformationContent(string $paymentPart): string
115
    {
116
        $furtherInformationContent = '';
117
118
        foreach($this->getFurtherInformationElements() as $furtherInformationElement) {
119
            $furtherInformationContent .= $this->getContentElement($furtherInformationElement);
120
        }
121
122
        $paymentPart = str_replace('{{ further-information-content }}', $furtherInformationContent, $paymentPart);
123
124
        return $paymentPart;
125
    }
126
127
    private function addPrintableContent(string $paymentPart): string
128
    {
129
        $printableStyles = '';
130
        if ($this->isPrintable()) {
131
            $printableStyles = PrintableStylesTemplate::TEMPLATE;
132
        }
133
134
        $paymentPart = str_replace('{{ printable-content }}', $printableStyles, $paymentPart);
135
136
        return $paymentPart;
137
    }
138
139
    private function getContentElement(OutputElementInterface $element): string
140
    {
141
        if ($element instanceof Title) {
142
            $elementTemplate = TitleElementTemplate::TEMPLATE;
143
            $elementString = str_replace('{{ title }}', $element->getTitle(), $elementTemplate);
144
145
            return $elementString;
146
        }
147
148
        if ($element instanceof Text) {
149
            $elementTemplate = TextElementTemplate::TEMPLATE;
150
            $elementString = str_replace('{{ text }}', nl2br($element->getText()), $elementTemplate);
151
152
            return $elementString;
153
        }
154
155
        if ($element instanceof Placeholder) {
156
            $elementTemplate = PlaceholderElementTemplate::TEMPLATE;
157
            $elementString = $elementTemplate;
158
159
            $svgDoc = new \DOMDocument();
160
            $svgDoc->loadXML(file_get_contents($element->getFile()));
161
            $svg = $svgDoc->getElementsByTagName('svg');
162
            $dataUri = 'data:image/svg+xml;base64,' . base64_encode($svg->item(0)->C14N());
163
164
            $elementString = str_replace('{{ file }}', $dataUri, $elementString);
165
            $elementString = str_replace('{{ width }}', $element->getWidth(), $elementString);
166
            $elementString = str_replace('{{ height }}', $element->getHeight(), $elementString);
167
            $elementString = str_replace('{{ id }}', $element->getType(), $elementString);
168
169
            return $elementString;
170
        }
171
    }
172
173
    private function translateContents($paymentPart, $language)
174
    {
175
        $translations = Translation::getAllByLanguage($language);
176
        foreach($translations as $key => $text) {
177
            $paymentPart = str_replace('{{ text.' . $key . ' }}', $text, $paymentPart);
178
        }
179
180
        return $paymentPart;
181
    }
182
}