Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.24% covered (success)
95.24%
20 / 21
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Footnotes
95.24% covered (success)
95.24%
20 / 21
50.00% covered (danger)
50.00%
1 / 2
10
0.00% covered (danger)
0.00%
0 / 1
 read
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
7
 getElement
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
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 * Footnotes reader.
26 *
27 * @since 0.10.0
28 */
29class Footnotes extends AbstractPart
30{
31    /**
32     * Collection name footnotes|endnotes.
33     *
34     * @var string
35     */
36    protected $collection = 'footnotes';
37
38    /**
39     * Element name footnote|endnote.
40     *
41     * @var string
42     */
43    protected $element = 'footnote';
44
45    /**
46     * Read (footnotes|endnotes).xml.
47     */
48    public function read(PhpWord $phpWord): void
49    {
50        $xmlReader = new XMLReader();
51        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
52        $nodes = $xmlReader->getElements('*');
53        if ($nodes->length > 0) {
54            foreach ($nodes as $node) {
55                $id = $xmlReader->getAttribute('w:id', $node);
56                $type = $xmlReader->getAttribute('w:type', $node);
57
58                // Avoid w:type "separator" and "continuationSeparator"
59                // Only look for <footnote> or <endnote> without w:type attribute, or with w:type = normal
60                if ((null === $type || $type === 'normal')) {
61                    $element = $this->getElement($phpWord, $id);
62                    if ($element !== null) {
63                        $pNodes = $xmlReader->getElements('w:p/*', $node);
64                        foreach ($pNodes as $pNode) {
65                            $this->readRun($xmlReader, $pNode, $element, $this->collection);
66                        }
67                        $addMethod = "add{$this->element}";
68                        $phpWord->$addMethod($element);
69                    }
70                }
71            }
72        }
73    }
74
75    /**
76     * Searches for the element with the given relationId.
77     *
78     * @param int $relationId
79     *
80     * @return null|\PhpOffice\PhpWord\Element\AbstractContainer
81     */
82    private function getElement(PhpWord $phpWord, $relationId)
83    {
84        $getMethod = "get{$this->collection}";
85        $collection = $phpWord->$getMethod()->getItems();
86
87        //not found by key, looping to search by relationId
88        foreach ($collection as $collectionElement) {
89            if ($collectionElement->getRelationId() == $relationId) {
90                return $collectionElement;
91            }
92        }
93
94        return null;
95    }
96}