Passed
Push — master ( 7a576b...99159c )
by zyt
04:01
created

GoogleFont::fromString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ZWF;
4
5
class GoogleFont
6
{
7
8
    protected $name = '';
9
10
    protected $sizes = [];
11
12
    protected $subsets = [];
13
14 5
    public function __construct($name, array $sizes = [], array $subsets = [])
15 5
    {
16 5
        $this->setName($name);
17 5
        $this->setSizes($sizes);
18 5
        $this->setSubsets($subsets);
19 5
    }
20
21 5
    public function setName($name)
22 5
    {
23 5
        $this->name = $name;
24 5
    }
25
26 5
    public function getName()
27 5
    {
28 5
        return $this->name;
29
    }
30
31
    /**
32
     * @param string|array $sizes
33
     */
34 5 View Code Duplication
    public function setSizes($sizes)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35 5
    {
36 5
        if (is_array($sizes)) {
37 5
            $this->sizes = $sizes;
38
        }
39 5
        if (is_string($sizes)) {
40 4
            $this->sizes = array_map('trim', explode(',', $sizes));
41
        }
42 5
    }
43
44 5
    public function getSizes()
45 5
    {
46 5
        return $this->sizes;
47
    }
48
49
    public function getSizesString()
50
    {
51
        return implode(',', $this->getSizes());
52
    }
53
54
    /**
55
     * @param string|array $subsets
56
     */
57 5 View Code Duplication
    public function setSubsets($subsets)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58 5
    {
59 5
        if (is_array($subsets)) {
60 5
            $this->subsets = $subsets;
61
        }
62 5
        if (is_string($subsets)) {
63 4
            $this->subsets = array_map('trim', explode(',', $subsets));
64
        }
65 5
    }
66
67 3
    public function getSubsets()
68 3
    {
69 3
        return $this->subsets;
70
    }
71
72 5
    public function getSubsetsString()
73 5
    {
74 5
        return implode(',', $this->subsets);
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function __toString()
81
    {
82
        return implode(
83
            ':',
84
            [
85
                $this->getName(),
86
                $this->getSizesString(),
87
                $this->getSubsetsString()
88
            ]
89
        );
90
    }
91
92
    /**
93
     * Creates a new instance from a given $fontstring.
94
     *
95
     * @param string $fontstring
96
     *
97
     * @return GoogleFont
98
     */
99 5
    public static function fromString($fontstring)
100 5
    {
101 5
        $parts = explode(':', $fontstring);
102 5
        $font  = new self($parts[0]);
103
104 5
        if (isset($parts[1])) {
105 4
            $font->setSizes($parts[1]);
106
        }
107
108 5
        if (isset($parts[2])) {
109 4
            $font->setSubsets($parts[2]);
110
        }
111
112 5
        return $font;
113
    }
114
}
115