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