Completed
Push — master ( 1391ca...439136 )
by Stefan
02:49
created

WidthCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace OneSheet\Width;
4
5
/**
6
 * Class WidthCollection
7
 *
8
 * @package OneSheet
9
 */
10
class WidthCollection
11
{
12
    /**
13
     * Array containing character widths for each font & size.
14
     *
15
     * @var array
16
     */
17
    private static $widths = array();
18
19
    /**
20
     * Create character width map for each font.
21
     */
22 13
    public function __construct()
23
    {
24 13
        self::loadWidthsFromCsv(dirname(__FILE__) . '/width_collection.csv');
25 13
    }
26
27
    /**
28
     * Dirty way to allow developers to load character widths that
29
     * are not yet included.
30
     *
31
     * @param string $csvPath
32
     */
33 13
    public static function loadWidthsFromCsv($csvPath)
34
    {
35 13
        $fh = fopen($csvPath, 'r');
36 13
        $head = fgetcsv($fh);
37 13
        unset($head[0], $head[1]);
38 13
        while ($row = fgetcsv($fh)) {
39 13
            $fontName = array_shift($row);
40 13
            $fontSize = array_shift($row);
41 13
            self::$widths[$fontName][$fontSize] = array_combine($head, $row);
42 13
        }
43 13
    }
44
45
    /**
46
     * Return character widths for given font name.
47
     *
48
     * @param string $fontName
49
     * @param int    $fontSize
50
     * @return array
51
     */
52 4
    public function get($fontName, $fontSize)
53
    {
54 4
        if (isset(self::$widths[$fontName][$fontSize])) {
55 4
            return self::$widths[$fontName][$fontSize];
56 1
        } elseif (isset(self::$widths['Calibri'][$fontSize])) {
57 1
            return self::$widths['Calibri'][$fontSize];
58
        }
59
60
        return self::$widths['Calibri'][11];
61
    }
62
}
63