Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
32 / 32 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Section | |
100.00% |
32 / 32 |
|
100.00% |
11 / 11 |
21 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| setStyle | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getStyle | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addFooter | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFooters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getFootnoteProperties | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setFootnoteProperties | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasDifferentFirstPage | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| addHeaderFooter | |
100.00% |
11 / 11 |
|
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 | |
| 19 | namespace PhpOffice\PhpWord\Element; |
| 20 | |
| 21 | use Exception; |
| 22 | use PhpOffice\PhpWord\ComplexType\FootnoteProperties; |
| 23 | use PhpOffice\PhpWord\Style\Section as SectionStyle; |
| 24 | |
| 25 | class 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 | public function setStyle(?array $style = null): void |
| 80 | { |
| 81 | if (null !== $style) { |
| 82 | $this->style->setStyleByArray($style); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get section style. |
| 88 | * |
| 89 | * @return ?SectionStyle |
| 90 | */ |
| 91 | public function getStyle() |
| 92 | { |
| 93 | return $this->style; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Add header. |
| 98 | * |
| 99 | * @since 0.10.0 |
| 100 | * |
| 101 | * @param string $type |
| 102 | * |
| 103 | * @return Header |
| 104 | */ |
| 105 | public function addHeader($type = Header::AUTO) |
| 106 | { |
| 107 | return $this->addHeaderFooter($type, true); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Add footer. |
| 112 | * |
| 113 | * @since 0.10.0 |
| 114 | * |
| 115 | * @param string $type |
| 116 | * |
| 117 | * @return Footer |
| 118 | */ |
| 119 | public function addFooter($type = Header::AUTO) |
| 120 | { |
| 121 | return $this->addHeaderFooter($type, false); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get header elements. |
| 126 | * |
| 127 | * @return Header[] |
| 128 | */ |
| 129 | public function getHeaders() |
| 130 | { |
| 131 | return $this->headers; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get footer elements. |
| 136 | * |
| 137 | * @return Footer[] |
| 138 | */ |
| 139 | public function getFooters() |
| 140 | { |
| 141 | return $this->footers; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get the footnote properties. |
| 146 | * |
| 147 | * @return FootnoteProperties |
| 148 | */ |
| 149 | public function getFootnoteProperties() |
| 150 | { |
| 151 | return $this->footnoteProperties; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Set the footnote properties. |
| 156 | */ |
| 157 | public function setFootnoteProperties(?FootnoteProperties $footnoteProperties = null): void |
| 158 | { |
| 159 | $this->footnoteProperties = $footnoteProperties; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Is there a header for this section that is for the first page only? |
| 164 | * |
| 165 | * If any of the Header instances have a type of Header::FIRST then this method returns true. |
| 166 | * False otherwise. |
| 167 | * |
| 168 | * @return bool |
| 169 | */ |
| 170 | public function hasDifferentFirstPage() |
| 171 | { |
| 172 | foreach ($this->headers as $header) { |
| 173 | if ($header->getType() == Header::FIRST) { |
| 174 | return true; |
| 175 | } |
| 176 | } |
| 177 | foreach ($this->footers as $footer) { |
| 178 | if ($footer->getType() == Header::FIRST) { |
| 179 | return true; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Add header/footer. |
| 188 | * |
| 189 | * @since 0.10.0 |
| 190 | * |
| 191 | * @param string $type |
| 192 | * @param bool $header |
| 193 | * |
| 194 | * @return Footer|Header |
| 195 | */ |
| 196 | private function addHeaderFooter($type = Header::AUTO, $header = true) |
| 197 | { |
| 198 | $containerClass = substr(static::class, 0, strrpos(static::class, '\\') ?: 0) . '\\' . |
| 199 | ($header ? 'Header' : 'Footer'); |
| 200 | $collectionArray = $header ? 'headers' : 'footers'; |
| 201 | $collection = &$this->$collectionArray; |
| 202 | |
| 203 | if (in_array($type, [Header::AUTO, Header::FIRST, Header::EVEN])) { |
| 204 | $index = count($collection); |
| 205 | /** @var AbstractContainer $container Type hint */ |
| 206 | $container = new $containerClass($this->sectionId, ++$index, $type); |
| 207 | $container->setPhpWord($this->phpWord); |
| 208 | |
| 209 | $collection[$index] = $container; |
| 210 | |
| 211 | return $container; |
| 212 | } |
| 213 | |
| 214 | throw new Exception('Invalid header/footer type.'); |
| 215 | } |
| 216 | } |