Completed
Push — develop ( 7a734f...e9a151 )
by Stefan
03:10
created

WidthCalculator::getCellWidth()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.2
cc 4
eloc 8
nc 6
nop 2
crap 4.0218
1
<?php
2
3
namespace OneSheet\Width;
4
5
use OneSheet\Style\Font;
6
7
class WidthCalculator
8
{
9
    /**
10
     * @var WidthCollection
11
     */
12
    private $widthCollection;
13
14
    /**
15
     * @var array
16
     */
17
    private $characterWidths;
18
19
    /**
20
     * Calculator constructor.
21
     */
22 5
    public function __construct()
23
    {
24 5
        $this->widthCollection = new WidthCollection();
25 5
    }
26
27
    /**
28
     * @param mixed $value
29
     * @param bool  $bold
30
     * @return float
31
     */
32 2
    public function getCellWidth($value, $bold)
33
    {
34 2
        $width = 1;
35 2
        foreach (str_split($value) as $character) {
36 2
            if (isset($this->characterWidths[$character])) {
37 2
                $width += $this->characterWidths[$character];
38 2
            } else {
39
                $width += 0.66;
40
            }
41 2
        }
42
43 2
        return !$bold ? $width : $width * 1.1;
44
    }
45
46
    /**
47
     * @param Font $font
48
     */
49 2
    public function setFont(Font $font)
50
    {
51 2
        $this->characterWidths = $this->widthCollection->get($font->getName(), $font->getSize());
52 2
    }
53
}
54