1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Docx template |
5
|
|
|
* |
6
|
|
|
* Platine Docx template is the lightweight library to manipulate the content of .docx files |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Docx template |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file PlatineTemplateRenderer.php |
33
|
|
|
* |
34
|
|
|
* The renderer using Platine Template class |
35
|
|
|
* |
36
|
|
|
* @package Platine\DocxTemplate\Renderer |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\DocxTemplate\Renderer; |
48
|
|
|
|
49
|
|
|
use Platine\DocxTemplate\DocxTemplateRendererInterface; |
50
|
|
|
use Platine\Template\Template; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @class PlatineTemplateRenderer |
54
|
|
|
* @package Platine\DocxTemplate\Renderer |
55
|
|
|
*/ |
56
|
|
|
class PlatineTemplateRenderer implements DocxTemplateRendererInterface |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* The template instance to use |
60
|
|
|
* @var Template |
61
|
|
|
*/ |
62
|
|
|
protected Template $template; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create new instance |
66
|
|
|
* @param Template $template |
67
|
|
|
*/ |
68
|
|
|
public function __construct(Template $template) |
69
|
|
|
{ |
70
|
|
|
$this->template = $template; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function render(string $content, array $data = []): string |
77
|
|
|
{ |
78
|
|
|
$fixSplitTagsContent = $this->fixSplitTemplateTags($content); |
79
|
|
|
$tableLoopHandle = $this->handleTableLoop($fixSplitTagsContent); |
80
|
|
|
|
81
|
|
|
return $this->template->renderString($tableLoopHandle, $data); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Clean the document XML tags |
86
|
|
|
* @param string $content |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
protected function fixSplitTemplateTags(string $content): string |
90
|
|
|
{ |
91
|
|
|
/* |
92
|
|
|
* If part of the tag is formatted differently we won't get a match. |
93
|
|
|
* Best explained with an example: |
94
|
|
|
* |
95
|
|
|
* ```xml |
96
|
|
|
* <w:r> |
97
|
|
|
* <w:rPr/> |
98
|
|
|
* <w:t>Hello ${tag_</w:t> |
99
|
|
|
* </w:r> |
100
|
|
|
* <w:r> |
101
|
|
|
* <w:rPr> |
102
|
|
|
* <w:b/> |
103
|
|
|
* <w:bCs/> |
104
|
|
|
* </w:rPr> |
105
|
|
|
* <w:t>1}</w:t> |
106
|
|
|
* </w:r> |
107
|
|
|
* ``` |
108
|
|
|
* |
109
|
|
|
* The above becomes, after running through this method: |
110
|
|
|
* |
111
|
|
|
* ```xml |
112
|
|
|
* <w:r> |
113
|
|
|
* <w:rPr/> |
114
|
|
|
* <w:t>Hello ${tag_1}</w:t> |
115
|
|
|
* </w:r> |
116
|
|
|
*/ |
117
|
|
|
$matches = []; |
118
|
|
|
preg_match_all('~\{(\{|%)\s*([^\}]+)\s*(\}|%)\}~U', $content, $matches); |
119
|
|
|
foreach ($matches[0] as $value) { |
120
|
|
|
$startTagsCleaned = (string) preg_replace('/<[^>]+>/', '', $value); |
121
|
|
|
$endTagsCleaned = (string) preg_replace('/<\/[^>]+>/', '', $startTagsCleaned); |
122
|
|
|
$content = str_replace($value, $endTagsCleaned, $content); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $content; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Handle table loop that contains the tags "{% for xx in yyyy %}" and "{% endfor %}" |
130
|
|
|
* @param string $content |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
protected function handleTableLoop(string $content): string |
134
|
|
|
{ |
135
|
|
|
$matches = []; |
136
|
|
|
preg_match_all('~<w:tr(.*?)>(.*)</w:tr>~si', $content, $matches); |
137
|
|
|
foreach ($matches[0] as $value) { |
138
|
|
|
$parts = explode('</w:tr>', $value); |
139
|
|
|
foreach ($parts as $tableRow) { |
140
|
|
|
$matchesStartLoop = []; |
141
|
|
|
preg_match( |
142
|
|
|
'~{%\s*for\s*([a-z0-9_]+)\s*in\s*([a-z0-9_]+)\s*%}~si', |
143
|
|
|
$tableRow, |
144
|
|
|
$matchesStartLoop |
145
|
|
|
); |
146
|
|
|
if (!empty($matchesStartLoop[0])) { |
147
|
|
|
$startLoop = $matchesStartLoop[0]; |
148
|
|
|
$newTableRow = $startLoop . str_replace($startLoop, '', $tableRow); |
149
|
|
|
$content = str_replace($tableRow, $newTableRow, $content); |
150
|
|
|
} |
151
|
|
|
if (strpos($tableRow, 'endfor') !== false) { |
152
|
|
|
$content = str_replace($tableRow . '</w:tr>', '{% endfor %}', $content); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $content; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|