Completed
Pull Request — master (#7)
by Stefan
03:14
created

WidthCollection   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 50
ccs 15
cts 16
cp 0.9375
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A loadWidthsFromCsv() 0 10 3
A get() 0 8 2
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 5
    public function __construct()
23
    {
24 5
        self::loadWidthsFromCsv(dirname(__FILE__) . '/widths.csv');
25 5
    }
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 5
    public static function loadWidthsFromCsv($csvPath)
34
    {
35 5
        foreach (file($csvPath) as $line) {
36 5
            $widths = explode(',', trim($line));
37 5
            if (count(range(33, 126)) + 2 == count($widths)) {
38 5
                self::$widths[array_shift($widths)][array_shift($widths)] =
39 5
                    array_combine(array_map('chr', range(33, 126)), $widths);
40 5
            }
41 5
        }
42 5
    }
43
44
    /**
45
     * Return character widths for given font name.
46
     *
47
     * @param string $fontName
48
     * @param int    $fontSize
49
     * @return array
50
     */
51 2
    public function get($fontName, $fontSize)
52
    {
53 2
        if (isset(self::$widths[$fontName][$fontSize])) {
54 2
            return self::$widths[$fontName][$fontSize];
55
        }
56
57
        return self::$widths['Calibri'][11];
58
    }
59
}
60