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