TransformMatcher   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 69
ccs 24
cts 26
cp 0.9231
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A makeSequence() 0 6 1
A matchRotate() 0 4 1
A matchPattern() 0 10 3
A matchSkewX() 0 4 1
A matchSkewY() 0 4 1
A matchScale() 0 4 1
A matchTranslate() 0 4 1
A matchMatrix() 0 11 2
1
<?php
2
namespace nstdio\svg\util\transform;
3
4
/**
5
 * Class TransformMatcher
6
 *
7
 * @package nstdio\svg\util
8
 * @author  Edgar Asatryan <[email protected]>
9
 */
10
class TransformMatcher implements TransformMatcherInterface
11
{
12
    const ROTATE_PATTERN = "/rotate\s*\(\s*(?<a>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<x>[+-]?\d+(?:\.\d+)?)(?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\s*\)/";
13
14
    const TRANSLATE_PATTERN = "/translate\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\)/";
15
16
    const SCALE_PATTERN = "/scale\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)((?:\s{1,}\,?\s*|\,\s*)(?<y>[+-]?\d+(?:\.\d+)?))?\)/";
17
18
    const SKEWX_PATTERN = "/skewX\s*\(\s*(?<x>[+-]?\d+(?:\.\d+)?)?\)/";
19
20
    const SKEWY_PATTERN = "/skewY\s*\(\s*(?<y>[+-]?\d+(?:\.\d+)?)?\)/";
21
22
    const MATRIX_PATTERN = "/matrix\s*\(\s*((([+-]?\d+(?:\.\d+)?)(?:\s+,?\s*|,\s*)){5}([+-]?\d+(?:\.\d+)?)\s*)\)/";
23
24 111
    public function makeSequence($transform)
25
    {
26 111
        preg_match_all("/\s*(matrix|translate|scale|rotate|skew[XY])/i", $transform, $matches);
27
28 111
        return $matches[1];
29
    }
30
31 7
    public function matchRotate($transform)
32
    {
33 7
        return $this->matchPattern(self::ROTATE_PATTERN, $transform, ['a', 'x', 'y']);
34
    }
35
36 9
    private function matchPattern($pattern, $transform, $named)
37
    {
38 9
        preg_match($pattern, $transform, $matches);
39 9
        $ret = [];
40 9
        foreach ($named as $value) {
41 9
            $ret[] = isset($matches[$value]) ? floatval($matches[$value]) : null;
42 9
        }
43
44 9
        return $ret;
45
    }
46
47 1
    public function matchSkewX($transform)
48
    {
49 1
        return $this->matchPattern(self::SKEWX_PATTERN, $transform, ['x']);
50
    }
51
52 1
    public function matchSkewY($transform)
53
    {
54 1
        return $this->matchPattern(self::SKEWY_PATTERN, $transform, ['y']);
55
    }
56
57 2
    public function matchScale($transform)
58
    {
59 2
        return $this->matchPattern(self::SCALE_PATTERN, $transform, ['x', 'y']);
60
    }
61
62 1
    public function matchTranslate($transform)
63
    {
64 1
        return $this->matchPattern(self::TRANSLATE_PATTERN, $transform, ['x', 'y']);
65
    }
66
67 2
    public function matchMatrix($transform)
68
    {
69 2
        preg_match(self::MATRIX_PATTERN, $transform, $matches);
70 2
        if (isset($matches[1]) === false) {
71 2
            throw new \InvalidArgumentException("Cannot match matrix transformation.");
72
        }
73
74
        $matrix = explode(' ', preg_replace(['/\s+/', '/\,+/'], [' ', ''], $matches[1]), 6);
75
76
        return $matrix;
77
    }
78
}