Completed
Push — master ( 6cea70...aebf2a )
by Edgar
04:08
created

TransformMatcher::makeSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace nstdio\svg\util;
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
    public function matchSkewX($transform)
37
    {
38
        return $this->matchPattern(self::SKEWX_PATTERN, $transform, ['x']);
39
    }
40
41
    public function matchSkewY($transform)
42
    {
43
        return $this->matchPattern(self::SKEWY_PATTERN, $transform, ['y']);
44
    }
45
46
    public function matchScale($transform)
47
    {
48
        return $this->matchPattern(self::SCALE_PATTERN, $transform, ['x', 'y']);
49
    }
50
51
52
    public function matchTranslate($transform)
53
    {
54
        return $this->matchPattern(self::TRANSLATE_PATTERN, $transform, ['x', 'y']);
55
    }
56
57
    public function matchMatrix($transform)
58
    {
59
        preg_match(self::MATRIX_PATTERN, $transform, $matches);
60
        if (isset($matches[1]) === false) {
61
            throw new \InvalidArgumentException("Cannot match matrix transformation.");
62
        }
63
64
        $matrix = explode(' ', preg_replace(['/\s+/', '/\,+/'], [' ', ''], $matches[1]), 6);
65
66
        return $matrix;
67
    }
68
69
    private function matchPattern($pattern, $transform, $named)
70
    {
71
        preg_match($pattern, $transform, $matches);
72
        $ret = [];
73
        foreach ($named as $value) {
74
            $ret[] = isset($matches[$value]) ? $matches[$value] : null;
75
        }
76
77
        return $ret;
78
    }
79
}