Utilities::interpretFont()   B
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 9
nop 3
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace EaselDrawing\Templates;
4
5
use EaselDrawing\Color;
6
use EaselDrawing\Font;
7
use EaselDrawing\Orientation;
8
9
class Utilities
10
{
11
12
    /**
13
     * @param array|string $value
14
     * @param Color $default
15
     * @return Color
16
     */
17
    public static function interpretColor($value, Color $default): Color
18
    {
19
        // color as RGB
20
        if (is_array($value)) {
21
            return Color::newFromArray($value);
22
        }
23
        // color as CSS hex definition
24
        if (is_string($value)) {
25
            return Color::newFromString($value);
26
        }
27
        return $default;
28
    }
29
30
    /**
31
     * @param string $name
32
     * @param string|array $data
33
     * @param PathResolver $pathResolver
34
     * @return Font
35
     */
36
    public static function interpretFont(string $name, $data, PathResolver $pathResolver): Font
37
    {
38
        if (is_string($data)) {
39
            /* Font defined as
40
             * Arial: /usr/share/fonts/truetype/Arial.ttf
41
             */
42
            $isBold = false;
43
            $isItalic = false;
44
            $location = $data;
45
        } elseif (is_array($data)) {
46
            /* Font defined as
47
             * Arial:
48
             *     - location: /usr/share/fonts/truetype/ArialBoldItalic.ttf
49
             *     - bold: false
50
             *     - italic: false
51
             */
52
            $isBold = (isset($data['bold'])) ? (bool) $data['bold'] : false;
53
            $isItalic = (isset($data['italic'])) ? (bool) $data['italic'] : false;
54
            $location = (isset($data['location'])) ? (string) $data['location'] : '';
55
        } else {
56
            throw new \InvalidArgumentException("Cannot interpret fonts as one of them is not a string or an array");
57
        }
58
        $location = $pathResolver->obtainPath($location);
59
60
        return new Font($name, $isBold, $isItalic, $location);
61
    }
62
63
    public static function interpretOrientation(string $orientation)
64
    {
65
        return Orientation::newFromString($orientation);
66
    }
67
}
68