Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Rotate
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toMatrix
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Phpdftk\Svg\Value\Transform;
6
7use Phpdftk\Svg\Value\TransformFunction;
8
9/**
10 * `rotate(angle)` or `rotate(angle, cx, cy)`. The angle is in degrees per
11 * SVG. The 3-arg form rotates around `(cx, cy)` — equivalent to
12 * `translate(cx, cy) rotate(angle) translate(-cx, -cy)`.
13 */
14final class Rotate implements TransformFunction
15{
16    public function __construct(
17        public readonly float $angle,
18        public readonly ?float $cx = null,
19        public readonly ?float $cy = null,
20    ) {}
21
22    public function toMatrix(): array
23    {
24        $rad = deg2rad($this->angle);
25        $cos = cos($rad);
26        $sin = sin($rad);
27        if ($this->cx === null && $this->cy === null) {
28            return [$cos, $sin, -$sin, $cos, 0.0, 0.0];
29        }
30        $cx = $this->cx ?? 0.0;
31        $cy = $this->cy ?? 0.0;
32        // M = T(cx,cy) · R(θ) · T(-cx,-cy); pre-folded so the painter
33        // doesn't have to compose three steps every paint call.
34        $e = $cx - $cos * $cx + $sin * $cy;
35        $f = $cy - $sin * $cx - $cos * $cy;
36        return [$cos, $sin, -$sin, $cos, $e, $f];
37    }
38}