Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
Style
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
12 / 12
19
100.00% covered (success)
100.00%
1 / 1
 addParagraphStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addFontStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addLinkStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addNumberingStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addTitleStyle
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 addTableStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 countStyles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 resetStyles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultParagraphStyle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStyles
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStyle
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setStyleValues
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
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;
19
20use PhpOffice\PhpWord\Style\AbstractStyle;
21use PhpOffice\PhpWord\Style\Font;
22use PhpOffice\PhpWord\Style\Numbering;
23use PhpOffice\PhpWord\Style\Paragraph;
24use PhpOffice\PhpWord\Style\Table;
25
26/**
27 * Style collection.
28 */
29class Style
30{
31    /**
32     * Style register.
33     *
34     * @var array
35     */
36    private static $styles = [];
37
38    /**
39     * Add paragraph style.
40     *
41     * @param string $styleName
42     * @param AbstractStyle|array $styles
43     *
44     * @return \PhpOffice\PhpWord\Style\Paragraph
45     */
46    public static function addParagraphStyle($styleName, $styles)
47    {
48        return self::setStyleValues($styleName, new Paragraph(), $styles);
49    }
50
51    /**
52     * Add font style.
53     *
54     * @param string $styleName
55     * @param AbstractStyle|array $fontStyle
56     * @param AbstractStyle|array $paragraphStyle
57     *
58     * @return \PhpOffice\PhpWord\Style\Font
59     */
60    public static function addFontStyle($styleName, $fontStyle, $paragraphStyle = null)
61    {
62        return self::setStyleValues($styleName, new Font('text', $paragraphStyle), $fontStyle);
63    }
64
65    /**
66     * Add link style.
67     *
68     * @param string $styleName
69     * @param AbstractStyle|array $styles
70     *
71     * @return \PhpOffice\PhpWord\Style\Font
72     */
73    public static function addLinkStyle($styleName, $styles)
74    {
75        return self::setStyleValues($styleName, new Font('link'), $styles);
76    }
77
78    /**
79     * Add numbering style.
80     *
81     * @param string $styleName
82     * @param AbstractStyle|array $styleValues
83     *
84     * @return \PhpOffice\PhpWord\Style\Numbering
85     *
86     * @since 0.10.0
87     */
88    public static function addNumberingStyle($styleName, $styleValues)
89    {
90        return self::setStyleValues($styleName, new Numbering(), $styleValues);
91    }
92
93    /**
94     * Add title style.
95     *
96     * @param null|int $depth Provide null to set title font
97     * @param AbstractStyle|array $fontStyle
98     * @param AbstractStyle|array $paragraphStyle
99     *
100     * @return \PhpOffice\PhpWord\Style\Font
101     */
102    public static function addTitleStyle($depth, $fontStyle, $paragraphStyle = null)
103    {
104        if (empty($depth)) {
105            $styleName = 'Title';
106        } else {
107            $styleName = "Heading_{$depth}";
108        }
109
110        return self::setStyleValues($styleName, new Font('title', $paragraphStyle), $fontStyle);
111    }
112
113    /**
114     * Add table style.
115     *
116     * @param string $styleName
117     * @param array $styleTable
118     * @param null|array $styleFirstRow
119     *
120     * @return \PhpOffice\PhpWord\Style\Table
121     */
122    public static function addTableStyle($styleName, $styleTable, $styleFirstRow = null)
123    {
124        return self::setStyleValues($styleName, new Table($styleTable, $styleFirstRow), null);
125    }
126
127    /**
128     * Count styles.
129     *
130     * @return int
131     *
132     * @since 0.10.0
133     */
134    public static function countStyles()
135    {
136        return count(self::$styles);
137    }
138
139    /**
140     * Reset styles.
141     *
142     * @since 0.10.0
143     */
144    public static function resetStyles(): void
145    {
146        self::$styles = [];
147    }
148
149    /**
150     * Set default paragraph style.
151     *
152     * @param AbstractStyle|array $styles Paragraph style definition
153     *
154     * @return \PhpOffice\PhpWord\Style\Paragraph
155     */
156    public static function setDefaultParagraphStyle($styles)
157    {
158        return self::addParagraphStyle('Normal', $styles);
159    }
160
161    /**
162     * Get all styles.
163     *
164     * @return AbstractStyle[]
165     */
166    public static function getStyles()
167    {
168        return self::$styles;
169    }
170
171    /**
172     * Get style by name.
173     *
174     * @param string $styleName
175     *
176     * @return ?AbstractStyle Paragraph|Font|Table|Numbering
177     */
178    public static function getStyle($styleName)
179    {
180        if (isset(self::$styles[$styleName])) {
181            return self::$styles[$styleName];
182        }
183
184        return null;
185    }
186
187    /**
188     * Set style values and put it to static style collection.
189     *
190     * The $styleValues could be an array or object
191     *
192     * @param string $name
193     * @param AbstractStyle $style
194     * @param AbstractStyle|array $value
195     *
196     * @return AbstractStyle
197     */
198    private static function setStyleValues($name, $style, $value = null)
199    {
200        if (!isset(self::$styles[$name])) {
201            if ($value !== null) {
202                if (is_array($value)) {
203                    $style->setStyleByArray($value);
204                } elseif ($value instanceof AbstractStyle) {
205                    if (get_class($style) == get_class($value)) {
206                        $style = $value;
207                    }
208                }
209            }
210            $style->setStyleName($name);
211            $style->setIndex(self::countStyles() + 1); // One based index
212            self::$styles[$name] = $style;
213        }
214
215        return self::getStyle($name);
216    }
217}