Completed
Pull Request — develop_3.0 (#433)
by Adrien
08:15
created

StyleManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 80
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaultStyle() 0 5 1
A registerStyle() 0 4 1
A applyExtraStylesIfNeeded() 0 5 1
B applyWrapTextIfCellContainsNewLine() 0 16 5
1
<?php
2
3
namespace Box\Spout\Writer\Common\Manager\Style;
4
5
use Box\Spout\Writer\Common\Entity\Style\Style;
6
7
/**
8
 * Class StyleManager
9
 * Manages styles to be applied to a cell
10
 *
11
 * @package Box\Spout\Writer\Common\Manager\Style
12
 */
13
class StyleManager implements StyleManagerInterface
14
{
15
    /** @var StyleRegistry Registry for all used styles */
16
    protected $styleRegistry;
17
18
    /**
19
     * @param StyleRegistry $styleRegistry
20
     */
21 100
    public function __construct(StyleRegistry $styleRegistry)
22
    {
23 100
        $this->styleRegistry = $styleRegistry;
24 100
    }
25
26
    /**
27
     * Returns the default style
28
     *
29
     * @return Style Default style
30
     */
31 34
    protected function getDefaultStyle()
32
    {
33
        // By construction, the default style has ID 0
34 34
        return $this->styleRegistry->getRegisteredStyles()[0];
35
    }
36
37
    /**
38
     * Registers the given style as a used style.
39
     * Duplicate styles won't be registered more than once.
40
     *
41
     * @param Style $style The style to be registered
42
     * @return Style The registered style, updated with an internal ID.
43
     */
44 64
    public function registerStyle($style)
45
    {
46 64
        return $this->styleRegistry->registerStyle($style);
47
    }
48
49
    /**
50
     * Apply additional styles if the given row needs it.
51
     * Typically, set "wrap text" if a cell contains a new line.
52
     *
53
     * @param Style $style The original style
54
     * @param array $dataRow The row the style will be applied to
55
     * @return Style The updated style
56
     */
57 66
    public function applyExtraStylesIfNeeded($style, $dataRow)
58
    {
59 66
        $updatedStyle = $this->applyWrapTextIfCellContainsNewLine($style, $dataRow);
60 66
        return $updatedStyle;
61
    }
62
63
    /**
64
     * Set the "wrap text" option if a cell of the given row contains a new line.
65
     *
66
     * @NOTE: There is a bug on the Mac version of Excel (2011 and below) where new lines
67
     *        are ignored even when the "wrap text" option is set. This only occurs with
68
     *        inline strings (shared strings do work fine).
69
     *        A workaround would be to encode "\n" as "_x000D_" but it does not work
70
     *        on the Windows version of Excel...
71
     *
72
     * @param Style $style The original style
73
     * @param array $dataRow The row the style will be applied to
74
     * @return Style The eventually updated style
75
     */
76 66
    protected function applyWrapTextIfCellContainsNewLine($style, $dataRow)
77
    {
78
        // if the "wrap text" option is already set, no-op
79 66
        if ($style->hasSetWrapText()) {
80 4
            return $style;
81
        }
82
83 63
        foreach ($dataRow as $cell) {
84 63
            if (is_string($cell) && strpos($cell, "\n") !== false) {
85 4
                $style->setShouldWrapText();
86 4
                break;
87
            }
88
        }
89
90 63
        return $style;
91
    }
92
}
93