EyeFill.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types = 1);
  3. namespace BaconQrCode\Renderer\RendererStyle;
  4. use BaconQrCode\Exception\RuntimeException;
  5. use BaconQrCode\Renderer\Color\ColorInterface;
  6. final class EyeFill
  7. {
  8. /**
  9. * @var ColorInterface|null
  10. */
  11. private $externalColor;
  12. /**
  13. * @var ColorInterface|null
  14. */
  15. private $internalColor;
  16. /**
  17. * @var self|null
  18. */
  19. private static $inherit;
  20. public function __construct(?ColorInterface $externalColor, ?ColorInterface $internalColor)
  21. {
  22. $this->externalColor = $externalColor;
  23. $this->internalColor = $internalColor;
  24. }
  25. public static function uniform(ColorInterface $color) : self
  26. {
  27. return new self($color, $color);
  28. }
  29. public static function inherit() : self
  30. {
  31. return self::$inherit ?: self::$inherit = new self(null, null);
  32. }
  33. public function inheritsBothColors() : bool
  34. {
  35. return null === $this->externalColor && null === $this->internalColor;
  36. }
  37. public function inheritsExternalColor() : bool
  38. {
  39. return null === $this->externalColor;
  40. }
  41. public function inheritsInternalColor() : bool
  42. {
  43. return null === $this->internalColor;
  44. }
  45. public function getExternalColor() : ColorInterface
  46. {
  47. if (null === $this->externalColor) {
  48. throw new RuntimeException('External eye color inherits foreground color');
  49. }
  50. return $this->externalColor;
  51. }
  52. public function getInternalColor() : ColorInterface
  53. {
  54. if (null === $this->internalColor) {
  55. throw new RuntimeException('Internal eye color inherits foreground color');
  56. }
  57. return $this->internalColor;
  58. }
  59. }