Code Coverage  | 
      ||||||||||
Lines  | 
       Functions and Methods  | 
       Classes and Traits  | 
      ||||||||
| Total |         | 
       100.00%  | 
       39 / 39  | 
               | 
       100.00%  | 
       2 / 2  | 
       CRAP |         | 
       100.00%  | 
       1 / 1  | 
      
| ContentTypes |         | 
       100.00%  | 
       39 / 39  | 
               | 
       100.00%  | 
       2 / 2  | 
       8 |         | 
       100.00%  | 
       1 / 1  | 
      
| write |         | 
       100.00%  | 
       32 / 32  | 
               | 
       100.00%  | 
       1 / 1  | 
       4 | |||
| writeContentType |         | 
       100.00%  | 
       7 / 7  | 
               | 
       100.00%  | 
       1 / 1  | 
       4 | |||
| 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\Shared\XMLWriter; | 
| 22 | |
| 23 | /** | 
| 24 | * Word2007 contenttypes part writer: [Content_Types].xml. | 
| 25 | */ | 
| 26 | class ContentTypes extends AbstractPart | 
| 27 | { | 
| 28 | /** | 
| 29 | * Write part. | 
| 30 | * | 
| 31 | * @return string | 
| 32 | */ | 
| 33 | public function write() | 
| 34 | { | 
| 35 | /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Type hint */ | 
| 36 | $parentWriter = $this->getParentWriter(); | 
| 37 | $contentTypes = $parentWriter->getContentTypes(); | 
| 38 | |
| 39 | $openXMLPrefix = 'application/vnd.openxmlformats-'; | 
| 40 | $wordMLPrefix = $openXMLPrefix . 'officedocument.wordprocessingml.'; | 
| 41 | $drawingMLPrefix = $openXMLPrefix . 'officedocument.drawingml.'; | 
| 42 | $overrides = [ | 
| 43 | '/docProps/core.xml' => $openXMLPrefix . 'package.core-properties+xml', | 
| 44 | '/docProps/app.xml' => $openXMLPrefix . 'officedocument.extended-properties+xml', | 
| 45 | '/docProps/custom.xml' => $openXMLPrefix . 'officedocument.custom-properties+xml', | 
| 46 | '/word/document.xml' => $wordMLPrefix . 'document.main+xml', | 
| 47 | '/word/styles.xml' => $wordMLPrefix . 'styles+xml', | 
| 48 | '/word/numbering.xml' => $wordMLPrefix . 'numbering+xml', | 
| 49 | '/word/settings.xml' => $wordMLPrefix . 'settings+xml', | 
| 50 | '/word/theme/theme1.xml' => $openXMLPrefix . 'officedocument.theme+xml', | 
| 51 | '/word/webSettings.xml' => $wordMLPrefix . 'webSettings+xml', | 
| 52 | '/word/fontTable.xml' => $wordMLPrefix . 'fontTable+xml', | 
| 53 | '/word/comments.xml' => $wordMLPrefix . 'comments+xml', | 
| 54 | ]; | 
| 55 | |
| 56 | $defaults = $contentTypes['default']; | 
| 57 | if (!empty($contentTypes['override'])) { | 
| 58 | foreach ($contentTypes['override'] as $key => $val) { | 
| 59 | if ($val == 'chart') { | 
| 60 | $overrides[$key] = $drawingMLPrefix . $val . '+xml'; | 
| 61 | } else { | 
| 62 | $overrides[$key] = $wordMLPrefix . $val . '+xml'; | 
| 63 | } | 
| 64 | } | 
| 65 | } | 
| 66 | |
| 67 | $xmlWriter = $this->getXmlWriter(); | 
| 68 | |
| 69 | $xmlWriter->startDocument('1.0', 'UTF-8', 'yes'); | 
| 70 | $xmlWriter->startElement('Types'); | 
| 71 | $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types'); | 
| 72 | |
| 73 | $this->writeContentType($xmlWriter, $defaults, true); | 
| 74 | $this->writeContentType($xmlWriter, $overrides, false); | 
| 75 | |
| 76 | $xmlWriter->endElement(); // Types | 
| 77 | |
| 78 | return $xmlWriter->getData(); | 
| 79 | } | 
| 80 | |
| 81 | /** | 
| 82 | * Write content types element. | 
| 83 | * | 
| 84 | * @param XMLWriter $xmlWriter XML Writer | 
| 85 | * @param array $parts | 
| 86 | * @param bool $isDefault | 
| 87 | */ | 
| 88 | private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault): void | 
| 89 | { | 
| 90 | foreach ($parts as $partName => $contentType) { | 
| 91 | $partType = $isDefault ? 'Default' : 'Override'; | 
| 92 | $partAttribute = $isDefault ? 'Extension' : 'PartName'; | 
| 93 | $xmlWriter->startElement($partType); | 
| 94 | $xmlWriter->writeAttribute($partAttribute, $partName); | 
| 95 | $xmlWriter->writeAttribute('ContentType', $contentType); | 
| 96 | $xmlWriter->endElement(); | 
| 97 | } | 
| 98 | } | 
| 99 | } |