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

Transform::scale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace nstdio\svg\util\transform;
3
4
/**
5
 * Class Transform
6
 *
7
 * @package nstdio\svg\util
8
 * @author  Edgar Asatryan <[email protected]>
9
 */
10
final class Transform implements TransformInterface
11
{
12
    private $trans;
13
14
    private $data;
15
16
    private $sequence;
17
18
    private $argDelimiter = TransformInterface::TRANSFORM_ARG_DELIM_SPACE;
19
20
    /**
21
     * @var PackerInterface
22
     */
23
    private $packer;
24
25
    private function __construct()
26
    {
27
    }
28
29
    /**
30
     * Use this method to instantiate Transform class.
31
     *
32
     * @param null|string                    $transformString
33
     *
34
     * @param null|TransformMatcherInterface $matcher
35
     *
36
     * @param PackerInterface                $packer
0 ignored issues
show
Documentation introduced by
Should the type for parameter $packer not be null|PackerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
     *
38
     * @return Transform
39
     */
40
    public static function newInstance($transformString = null, TransformMatcherInterface $matcher = null,
41
                                       PackerInterface $packer = null)
42
    {
43
        $instance = new Transform();
44
        $matcherImpl = $matcher === null ? new TransformMatcher() : $matcher;
45
        $instance->packer = $packer === null ? new Packer() : $packer;
46
        $instance->trans = $transformString === null ? '' : $transformString;
47
        $instance->sequence = $matcherImpl->makeSequence($transformString);
48
49
        foreach ($instance->sequence as $value) {
50
            $method = 'match' . ucfirst($value);
51
            $instance->data[][$value] = $matcherImpl->$method($transformString);
52
        }
53
54
        return $instance;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function result()
61
    {
62
        return $this->trans;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function setArgumentDelimiter($delim)
69
    {
70
        if ($delim !== TransformInterface::TRANSFORM_ARG_DELIM_COMMA &&
71
            $delim !== TransformInterface::TRANSFORM_ARG_DELIM_COMMA_SPACE &&
72
            $delim !== TransformInterface::TRANSFORM_ARG_DELIM_SPACE
73
        ) {
74
            throw new \InvalidArgumentException("Invalid delimiter. See TransformInterface::setArgumentDelimiter documentation for valid value.");
75
        }
76
77
        $this->argDelimiter = $delim;
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function rotate($angle, $cx = null, $cy = null)
84
    {
85
        if ($cx !== null && $cy === null) {
86
            $cy = $cx;
87
        }
88
        if ($cy !== null && $cx === null) {
89
            $cx = $cy;
90
        }
91
92
        return $this->shortcutBuild('rotate', [floatval($angle), $cx, $cy]);
93
    }
94
95
    private function shortcutBuild($transform, $data)
96
    {
97
        $this->sequence[] = $transform;
98
99
        foreach ($data as $key => $item) {
100
            if ($item !== null && !is_float($item) && !is_int($item)) {
101
                $data[$key] = floatval($item);
102
            }
103
        }
104
        $this->data[][$transform] = $data;
105
106
        return $this->buildTransformString();
107
    }
108
109
    private function buildTransformString()
110
    {
111
        $ret = '';
112
        foreach ($this->data as $key => $data) {
113
            $transform = array_keys($data)[0];
114
115
            $ret .= $transform . "(" . rtrim(implode($this->argDelimiter, $data[$transform])) . ") ";
116
        }
117
118
        $this->trans = rtrim($ret);
119
120
        return $this->trans;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function translate($x, $y = null)
127
    {
128
        return $this->shortcutBuild('translate', [$x, $y]);
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function scale($x, $y = null)
135
    {
136
        return $this->shortcutBuild('scale', [floatval($x), $y]);
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function skewX($x)
143
    {
144
        return $this->shortcutBuild('skewX', [$x]);
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function skewY($y)
151
    {
152
        return $this->shortcutBuild('skewY', [$y]);
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158
    public function matrix(array $matrix)
159
    {
160
        if (count($matrix) !== 6) {
161
            throw new \InvalidArgumentException("Invalid matrix size. You must provide en array with 6 elements. " . count($matrix) . " elements given.");
162
        }
163
164
        return $this->shortcutBuild('matrix', $matrix);
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function getData()
171
    {
172
        return $this->data;
173
    }
174
175
    /**
176
     * @inheritdoc
177
     */
178
    public function compact()
179
    {
180
        $this->data = $this->packer->pack($this->data);
181
182
        return $this->buildTransformString();
183
    }
184
185
    /**
186
     * Converts all transformations to matrix.
187
     *
188
     * @return string The converted to matrix string transformation.
189
     */
190
    public function toMatrix()
191
    {
192
        $this->data = $this->packer->toMatrix($this->data);
193
194
        return $this->buildTransformString();
195
    }
196
}