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