Passed
Pull Request — develop_3.0 (#485)
by Adrien
03:18
created

StyleManager::getDefaultStyle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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