SquareModule.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCode\Renderer\Module;
  4. use BaconQrCode\Encoder\ByteMatrix;
  5. use BaconQrCode\Renderer\Module\EdgeIterator\EdgeIterator;
  6. use BaconQrCode\Renderer\Path\Path;
  7. /**
  8. * Groups modules together to a single path.
  9. */
  10. final class SquareModule implements ModuleInterface
  11. {
  12. /**
  13. * @var self|null
  14. */
  15. private static $instance;
  16. private function __construct()
  17. {
  18. }
  19. public static function instance() : self
  20. {
  21. return self::$instance ?: self::$instance = new self();
  22. }
  23. public function createPath(ByteMatrix $matrix) : Path
  24. {
  25. $path = new Path();
  26. foreach (new EdgeIterator($matrix) as $edge) {
  27. $points = $edge->getSimplifiedPoints();
  28. $length = count($points);
  29. $path = $path->move($points[0][0], $points[0][1]);
  30. for ($i = 1; $i < $length; ++$i) {
  31. $path = $path->line($points[$i][0], $points[$i][1]);
  32. }
  33. $path = $path->close();
  34. }
  35. return $path;
  36. }
  37. }