Failed Conditions
Pull Request — master (#393)
by Stefan
02:59
created

SizeCollection::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Box\Spout\Writer\XLSX\Helper;
4
5
/**
6
 * SizeCollection to build and hold widths & heights of individual characters.
7
 */
8
class SizeCollection
9
{
10
    /** Constant for default character size. */
11
    const BASE_SIZE = 12;
12
13
    /** @var array Contains character widths/heights for each font & size. */
14
    private $sizes = array();
15
16
    /**
17
     * SizeCollection constructor to read character sizes from csv.
18
     */
19 141
    public function __construct()
20
    {
21 141
        $fh = fopen(dirname(__FILE__) . '/size_collection.csv', 'r');
22 141
        $head = fgetcsv($fh);
23 141
        unset($head[0], $head[1]);
24
25 141
        while ($row = fgetcsv($fh)) {
26 141
            $this->addSizesToCollection($head, $row);
27 141
        }
28 141
    }
29
30
    /**
31
     * Return character sizes for given font name.
32
     *
33
     * @param string $fontName
34
     * @param int    $fontSize
35
     * @return array
36
     */
37 9
    public function get($fontName, $fontSize)
38
    {
39 9
        if (isset($this->sizes[$fontName][$fontSize])) {
40 6
            return $this->sizes[$fontName][$fontSize];
41
        }
42
43 6
        return $this->calculate($fontName, $fontSize);
44
    }
45
46
    /**
47
     * Calculate character widths based on font name and size.
48
     *
49
     * @param string $fontName
50
     * @param int    $fontSize
51
     * @return array
52
     */
53 6
    private function calculate($fontName, $fontSize)
54
    {
55 6
        foreach ($this->getBaseSizes($fontName) as $character => $size) {
56 6
            $size = round($size / self::BASE_SIZE * $fontSize, 3);
57 6
            $this->sizes[$fontName][$fontSize][$character] = $size;
58 6
        }
59 6
        return $this->sizes[$fontName][$fontSize];
60
    }
61
62
    /**
63
     * Get character base widths by font name or default.
64
     *
65
     * @param string $fontName
66
     * @return array
67
     */
68 6
    private function getBaseSizes($fontName)
69
    {
70 6
        if (isset($this->sizes[$fontName])) {
71 3
            return $this->sizes[$fontName][self::BASE_SIZE];
72
        }
73 3
        return $this->sizes['Calibri'][self::BASE_SIZE];
74
    }
75
76
    /**
77
     * Add character widths for a single font.
78
     *
79
     * @param array $keys
80
     * @param array $values
81
     */
82 141
    private function addSizesToCollection(array $keys, array $values)
83
    {
84 141
        $fontName = array_shift($values);
85 141
        $fontSize = array_shift($values);
86 141
        $this->sizes[$fontName][$fontSize] = array_combine($keys, $values);
87
    }
88
}