Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
DocPropsCore
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 read
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
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
18namespace PhpOffice\PhpWord\Reader\Word2007;
19
20use PhpOffice\PhpWord\PhpWord;
21use PhpOffice\PhpWord\Shared\XMLReader;
22
23/**
24 * Core properties reader.
25 *
26 * @since 0.10.0
27 */
28class DocPropsCore extends AbstractPart
29{
30    /**
31     * Property mapping.
32     *
33     * @var array
34     */
35    protected $mapping = [
36        'dc:creator' => 'setCreator',
37        'dc:title' => 'setTitle',
38        'dc:description' => 'setDescription',
39        'dc:subject' => 'setSubject',
40        'cp:keywords' => 'setKeywords',
41        'cp:category' => 'setCategory',
42        'cp:lastModifiedBy' => 'setLastModifiedBy',
43        'dcterms:created' => 'setCreated',
44        'dcterms:modified' => 'setModified',
45    ];
46
47    /**
48     * Callback functions.
49     *
50     * @var array
51     */
52    protected $callbacks = ['dcterms:created' => 'strtotime', 'dcterms:modified' => 'strtotime'];
53
54    /**
55     * Read core/extended document properties.
56     */
57    public function read(PhpWord $phpWord): void
58    {
59        $xmlReader = new XMLReader();
60        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
61
62        $docProps = $phpWord->getDocInfo();
63
64        $nodes = $xmlReader->getElements('*');
65        if ($nodes->length > 0) {
66            foreach ($nodes as $node) {
67                if (!isset($this->mapping[$node->nodeName])) {
68                    continue;
69                }
70                $method = $this->mapping[$node->nodeName];
71                $value = $node->nodeValue == '' ? null : $node->nodeValue;
72                if (isset($this->callbacks[$node->nodeName])) {
73                    $value = $this->callbacks[$node->nodeName]($value);
74                }
75                if (method_exists($docProps, $method)) {
76                    $docProps->$method($value);
77                }
78            }
79        }
80    }
81}