Test Failed
Push — master ( b39eb7...589279 )
by Edgar
03:49
created

TransformMatcher   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 69
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
    public function makeSequence($transform)
25
    {
26
        preg_match_all("/\s*(matrix|translate|scale|rotate|skew[XY])/i", $transform, $matches);
27
28
        return $matches[1];
29
    }
30
31
    public function matchRotate($transform)
32
    {
33
        return $this->matchPattern(self::ROTATE_PATTERN, $transform, ['a', 'x', 'y']);
34
    }
35
36
    private function matchPattern($pattern, $transform, $named)
37
    {
38
        preg_match($pattern, $transform, $matches);
39
        $ret = [];
40
        foreach ($named as $value) {
41
            $ret[] = isset($matches[$value]) ? floatval($matches[$value]) : null;
42
        }
43
44
        return $ret;
45
    }
46
47
    public function matchSkewX($transform)
48
    {
49
        return $this->matchPattern(self::SKEWX_PATTERN, $transform, ['x']);
50
    }
51
52
    public function matchSkewY($transform)
53
    {
54
        return $this->matchPattern(self::SKEWY_PATTERN, $transform, ['y']);
55
    }
56
57
    public function matchScale($transform)
58
    {
59
        return $this->matchPattern(self::SCALE_PATTERN, $transform, ['x', 'y']);
60
    }
61
62
    public function matchTranslate($transform)
63
    {
64
        return $this->matchPattern(self::TRANSLATE_PATTERN, $transform, ['x', 'y']);
65
    }
66
67
    public function matchMatrix($transform)
68
    {
69
        preg_match(self::MATRIX_PATTERN, $transform, $matches);
70
        if (isset($matches[1]) === false) {
71
            throw new \InvalidArgumentException("Cannot match matrix transformation.");
72
        }
73
74
        $matrix = explode(' ', preg_replace(['/\s+/', '/\,+/'], [' ', ''], $matches[1]), 6);
75
76
        return $matrix;
77
    }
78
}