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