Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
24 / 28
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Border
85.71% covered (warning)
85.71%
24 / 28
75.00% covered (warning)
75.00%
3 / 4
10.29
0.00% covered (danger)
0.00%
0 / 1
 write
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 writeSide
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
4.30
 setSizes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setColors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\Writer\RTF\Style;
19
20/**
21 * Border style writer.
22 *
23 * @since 0.12.0
24 */
25class Border extends AbstractStyle
26{
27    /**
28     * Sizes.
29     *
30     * @var array
31     */
32    private $sizes = [];
33
34    /**
35     * Colors.
36     *
37     * @var array
38     */
39    private $colors = [];
40
41    /**
42     * Write style.
43     *
44     * @return string
45     */
46    public function write()
47    {
48        $content = '';
49
50        $sides = ['top', 'left', 'right', 'bottom'];
51        $sizeCount = count($this->sizes);
52
53        // Page border measure
54        // 8 = from text, infront off; 32 = from edge, infront on; 40 = from edge, infront off
55        $content .= '\pgbrdropt32';
56
57        for ($i = 0; $i < $sizeCount; ++$i) {
58            if ($this->sizes[$i] !== null) {
59                $color = null;
60                if (isset($this->colors[$i])) {
61                    $color = $this->colors[$i];
62                }
63                $content .= $this->writeSide($sides[$i], $this->sizes[$i], $color);
64            }
65        }
66
67        return $content;
68    }
69
70    /**
71     * Write side.
72     *
73     * @param string $side
74     * @param int $width
75     * @param string $color
76     *
77     * @return string
78     */
79    private function writeSide($side, $width, $color = '')
80    {
81        /** @var \PhpOffice\PhpWord\Writer\RTF $rtfWriter */
82        $rtfWriter = $this->getParentWriter();
83        $colorIndex = 0;
84        if ($rtfWriter !== null) {
85            $colorTable = $rtfWriter->getColorTable();
86            $index = array_search($color, $colorTable);
87            if ($index !== false && $colorIndex !== null) {
88                $colorIndex = $index + 1;
89            }
90        }
91
92        $content = '';
93
94        $content .= '\pgbrdr' . substr($side, 0, 1);
95        $content .= '\brdrs'; // Single-thickness border; @todo Get other type of border
96        $content .= '\brdrw' . round($width); // Width
97        $content .= '\brdrcf' . $colorIndex; // Color
98        $content .= '\brsp480'; // Space in twips between borders and the paragraph (24pt, following OOXML)
99        $content .= ' ';
100
101        return $content;
102    }
103
104    /**
105     * Set sizes.
106     *
107     * @param int[] $value
108     */
109    public function setSizes($value): void
110    {
111        $this->sizes = $value;
112    }
113
114    /**
115     * Set colors.
116     *
117     * @param string[] $value
118     */
119    public function setColors($value): void
120    {
121        $this->colors = $value;
122    }
123}