Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
37 / 37 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
Rels | |
100.00% |
37 / 37 |
|
100.00% |
4 / 4 |
11 | |
100.00% |
1 / 1 |
write | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
writeRels | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
writeMediaRel | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
writeRel | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Writer\Word2007\Part; |
19 | |
20 | use PhpOffice\PhpWord\Exception\Exception; |
21 | use PhpOffice\PhpWord\Shared\XMLWriter; |
22 | |
23 | /** |
24 | * Word2007 main relationship writer: _rels/.rels. |
25 | * |
26 | * @since 0.10.0 |
27 | */ |
28 | class Rels extends AbstractPart |
29 | { |
30 | /** |
31 | * Write part. |
32 | * |
33 | * @return string |
34 | */ |
35 | public function write() |
36 | { |
37 | $xmlRels = [ |
38 | 'docProps/core.xml' => 'package/2006/relationships/metadata/core-properties', |
39 | 'docProps/app.xml' => 'officeDocument/2006/relationships/extended-properties', |
40 | 'docProps/custom.xml' => 'officeDocument/2006/relationships/custom-properties', |
41 | 'word/document.xml' => 'officeDocument/2006/relationships/officeDocument', |
42 | ]; |
43 | $xmlWriter = $this->getXmlWriter(); |
44 | $this->writeRels($xmlWriter, $xmlRels); |
45 | |
46 | return $xmlWriter->getData(); |
47 | } |
48 | |
49 | /** |
50 | * Write relationships. |
51 | * |
52 | * @param array $xmlRels |
53 | * @param array $mediaRels |
54 | * @param int $relId |
55 | */ |
56 | protected function writeRels(XMLWriter $xmlWriter, $xmlRels = [], $mediaRels = [], $relId = 1): void |
57 | { |
58 | $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); |
59 | $xmlWriter->startElement('Relationships'); |
60 | $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); |
61 | |
62 | // XML files relationships |
63 | foreach ($xmlRels as $target => $type) { |
64 | $this->writeRel($xmlWriter, $relId++, $type, $target); |
65 | } |
66 | |
67 | // Media relationships |
68 | foreach ($mediaRels as $mediaRel) { |
69 | $this->writeMediaRel($xmlWriter, $relId++, $mediaRel); |
70 | } |
71 | |
72 | $xmlWriter->endElement(); // Relationships |
73 | } |
74 | |
75 | /** |
76 | * Write media relationships. |
77 | * |
78 | * @param int $relId |
79 | * @param array $mediaRel |
80 | */ |
81 | private function writeMediaRel(XMLWriter $xmlWriter, $relId, $mediaRel): void |
82 | { |
83 | $typePrefix = 'officeDocument/2006/relationships/'; |
84 | $typeMapping = ['image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink']; |
85 | $targetMapping = ['image' => 'media/', 'object' => 'embeddings/']; |
86 | |
87 | $mediaType = $mediaRel['type']; |
88 | $type = $typeMapping[$mediaType] ?? $mediaType; |
89 | $targetPrefix = $targetMapping[$mediaType] ?? ''; |
90 | $target = $mediaRel['target']; |
91 | $targetMode = ($type == 'hyperlink') ? 'External' : ''; |
92 | |
93 | $this->writeRel($xmlWriter, $relId, $typePrefix . $type, $targetPrefix . $target, $targetMode); |
94 | } |
95 | |
96 | /** |
97 | * Write individual rels entry. |
98 | * |
99 | * Format: |
100 | * <Relationship Id="rId..." Type="http://..." Target="....xml" TargetMode="..." /> |
101 | * |
102 | * @param int $relId Relationship ID |
103 | * @param string $type Relationship type |
104 | * @param string $target Relationship target |
105 | * @param string $targetMode Relationship target mode |
106 | */ |
107 | private function writeRel(XMLWriter $xmlWriter, $relId, $type, $target, $targetMode = ''): void |
108 | { |
109 | if ($type != '' && $target != '') { |
110 | if (strpos($relId, 'rId') === false) { |
111 | $relId = 'rId' . $relId; |
112 | } |
113 | $xmlWriter->startElement('Relationship'); |
114 | $xmlWriter->writeAttribute('Id', $relId); |
115 | $xmlWriter->writeAttribute('Type', 'http://schemas.openxmlformats.org/' . $type); |
116 | $xmlWriter->writeAttribute('Target', $target); |
117 | if ($targetMode != '') { |
118 | $xmlWriter->writeAttribute('TargetMode', $targetMode); |
119 | } |
120 | $xmlWriter->endElement(); |
121 | } else { |
122 | throw new Exception('Invalid parameters passed.'); |
123 | } |
124 | } |
125 | } |