Path.php 2.4 KB

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