Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Section
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
11 / 11
21
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setStyle
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 getStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addHeader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addFooter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHeaders
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFooters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFootnoteProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFootnoteProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasDifferentFirstPage
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 addHeaderFooter
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
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\Element;
19
20use Exception;
21use PhpOffice\PhpWord\ComplexType\FootnoteProperties;
22use PhpOffice\PhpWord\Style\Section as SectionStyle;
23
24class Section extends AbstractContainer
25{
26    /**
27     * @var string Container type
28     */
29    protected $container = 'Section';
30
31    /**
32     * Section style.
33     *
34     * @var ?\PhpOffice\PhpWord\Style\Section
35     */
36    private $style;
37
38    /**
39     * Section headers, indexed from 1, not zero.
40     *
41     * @var Header[]
42     */
43    private $headers = [];
44
45    /**
46     * Section footers, indexed from 1, not zero.
47     *
48     * @var Footer[]
49     */
50    private $footers = [];
51
52    /**
53     * The properties for the footnote of this section.
54     *
55     * @var FootnoteProperties
56     */
57    private $footnoteProperties;
58
59    /**
60     * Create new instance.
61     *
62     * @param int $sectionCount
63     * @param null|array|\PhpOffice\PhpWord\Style|string $style
64     */
65    public function __construct($sectionCount, $style = null)
66    {
67        $this->sectionId = $sectionCount;
68        $this->setDocPart($this->container, $this->sectionId);
69        if (null === $style) {
70            $style = new SectionStyle();
71        }
72        $this->style = $this->setNewStyle(new SectionStyle(), $style);
73    }
74
75    /**
76     * Set section style.
77     *
78     * @param array $style
79     */
80    public function setStyle($style = null): void
81    {
82        if (null !== $style && is_array($style)) {
83            $this->style->setStyleByArray($style);
84        }
85    }
86
87    /**
88     * Get section style.
89     *
90     * @return ?\PhpOffice\PhpWord\Style\Section
91     */
92    public function getStyle()
93    {
94        return $this->style;
95    }
96
97    /**
98     * Add header.
99     *
100     * @since 0.10.0
101     *
102     * @param string $type
103     *
104     * @return Header
105     */
106    public function addHeader($type = Header::AUTO)
107    {
108        return $this->addHeaderFooter($type, true);
109    }
110
111    /**
112     * Add footer.
113     *
114     * @since 0.10.0
115     *
116     * @param string $type
117     *
118     * @return Footer
119     */
120    public function addFooter($type = Header::AUTO)
121    {
122        return $this->addHeaderFooter($type, false);
123    }
124
125    /**
126     * Get header elements.
127     *
128     * @return Header[]
129     */
130    public function getHeaders()
131    {
132        return $this->headers;
133    }
134
135    /**
136     * Get footer elements.
137     *
138     * @return Footer[]
139     */
140    public function getFooters()
141    {
142        return $this->footers;
143    }
144
145    /**
146     * Get the footnote properties.
147     *
148     * @return FootnoteProperties
149     */
150    public function getFootnoteProperties()
151    {
152        return $this->footnoteProperties;
153    }
154
155    /**
156     * Set the footnote properties.
157     */
158    public function setFootnoteProperties(?FootnoteProperties $footnoteProperties = null): void
159    {
160        $this->footnoteProperties = $footnoteProperties;
161    }
162
163    /**
164     * Is there a header for this section that is for the first page only?
165     *
166     * If any of the Header instances have a type of Header::FIRST then this method returns true.
167     * False otherwise.
168     *
169     * @return bool
170     */
171    public function hasDifferentFirstPage()
172    {
173        foreach ($this->headers as $header) {
174            if ($header->getType() == Header::FIRST) {
175                return true;
176            }
177        }
178        foreach ($this->footers as $footer) {
179            if ($footer->getType() == Header::FIRST) {
180                return true;
181            }
182        }
183
184        return false;
185    }
186
187    /**
188     * Add header/footer.
189     *
190     * @since 0.10.0
191     *
192     * @param string $type
193     * @param bool $header
194     *
195     * @return Footer|Header
196     */
197    private function addHeaderFooter($type = Header::AUTO, $header = true)
198    {
199        $containerClass = substr(static::class, 0, strrpos(static::class, '\\')) . '\\' .
200            ($header ? 'Header' : 'Footer');
201        $collectionArray = $header ? 'headers' : 'footers';
202        $collection = &$this->$collectionArray;
203
204        if (in_array($type, [Header::AUTO, Header::FIRST, Header::EVEN])) {
205            $index = count($collection);
206            /** @var \PhpOffice\PhpWord\Element\AbstractContainer $container Type hint */
207            $container = new $containerClass($this->sectionId, ++$index, $type);
208            $container->setPhpWord($this->phpWord);
209
210            $collection[$index] = $container;
211
212            return $container;
213        }
214
215        throw new Exception('Invalid header/footer type.');
216    }
217}