| 1 |
<?php |
| 2 |
declare(strict_types = 1); |
| 3 |
|
| 4 |
namespace BaconQrCode\Renderer\Path; |
| 5 |
|
| 6 |
use IteratorAggregate; |
| 7 |
use Traversable; |
| 8 |
|
| 9 |
/** |
| 10 |
* Internal Representation of a vector path. |
| 11 |
*/ |
| 12 |
final class Path implements IteratorAggregate |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var OperationInterface[] |
| 16 |
*/ |
| 17 |
private array $operations = []; |
| 18 |
|
| 19 |
/** |
| 20 |
* Moves the drawing operation to a certain position. |
| 21 |
*/ |
| 22 |
public function move(float $x, float $y) : self |
| 23 |
{ |
| 24 |
$path = clone $this; |
| 25 |
$path->operations[] = new Move($x, $y); |
| 26 |
return $path; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Draws a line from the current position to another position. |
| 31 |
*/ |
| 32 |
public function line(float $x, float $y) : self |
| 33 |
{ |
| 34 |
$path = clone $this; |
| 35 |
$path->operations[] = new Line($x, $y); |
| 36 |
return $path; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Draws an elliptic arc from the current position to another position. |
| 41 |
*/ |
| 42 |
public function ellipticArc( |
| 43 |
float $xRadius, |
| 44 |
float $yRadius, |
| 45 |
float $xAxisRotation, |
| 46 |
bool $largeArc, |
| 47 |
bool $sweep, |
| 48 |
float $x, |
| 49 |
float $y |
| 50 |
) : self { |
| 51 |
$path = clone $this; |
| 52 |
$path->operations[] = new EllipticArc($xRadius, $yRadius, $xAxisRotation, $largeArc, $sweep, $x, $y); |
| 53 |
return $path; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Draws a curve from the current position to another position. |
| 58 |
*/ |
| 59 |
public function curve(float $x1, float $y1, float $x2, float $y2, float $x3, float $y3) : self |
| 60 |
{ |
| 61 |
$path = clone $this; |
| 62 |
$path->operations[] = new Curve($x1, $y1, $x2, $y2, $x3, $y3); |
| 63 |
return $path; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Closes a sub-path. |
| 68 |
*/ |
| 69 |
public function close() : self |
| 70 |
{ |
| 71 |
$path = clone $this; |
| 72 |
$path->operations[] = Close::instance(); |
| 73 |
return $path; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Appends another path to this one. |
| 78 |
*/ |
| 79 |
public function append(self $other) : self |
| 80 |
{ |
| 81 |
$path = clone $this; |
| 82 |
$path->operations = array_merge($this->operations, $other->operations); |
| 83 |
return $path; |
| 84 |
} |
| 85 |
|
| 86 |
public function translate(float $x, float $y) : self |
| 87 |
{ |
| 88 |
$path = new self(); |
| 89 |
|
| 90 |
foreach ($this->operations as $operation) { |
| 91 |
$path->operations[] = $operation->translate($x, $y); |
| 92 |
} |
| 93 |
|
| 94 |
return $path; |
| 95 |
} |
| 96 |
|
| 97 |
public function rotate(int $degrees) : self |
| 98 |
{ |
| 99 |
$path = new self(); |
| 100 |
|
| 101 |
foreach ($this->operations as $operation) { |
| 102 |
$path->operations[] = $operation->rotate($degrees); |
| 103 |
} |
| 104 |
|
| 105 |
return $path; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @return Traversable<int, OperationInterface> |
| 110 |
*/ |
| 111 |
public function getIterator() : Traversable |
| 112 |
{ |
| 113 |
foreach ($this->operations as $operation) { |
| 114 |
yield $operation; |
| 115 |
} |
| 116 |
} |
| 117 |
} |