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