QROutputAbstract.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Class QROutputAbstract
  4. *
  5. * @filesource QROutputAbstract.php
  6. * @created 09.12.2015
  7. * @package chillerlan\QRCode\Output
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode\Output;
  13. use chillerlan\QRCode\{Data\QRMatrix, QRCode};
  14. use chillerlan\Settings\SettingsContainerInterface;
  15. use function base64_encode, call_user_func_array, dirname, file_put_contents, get_called_class, in_array, is_writable, sprintf;
  16. /**
  17. * common output abstract
  18. */
  19. abstract class QROutputAbstract implements QROutputInterface{
  20. /**
  21. * the current size of the QR matrix
  22. *
  23. * @see \chillerlan\QRCode\Data\QRMatrix::size()
  24. */
  25. protected int $moduleCount;
  26. /**
  27. * the current output mode
  28. *
  29. * @see \chillerlan\QRCode\QROptions::$outputType
  30. */
  31. protected string $outputMode;
  32. /**
  33. * the default output mode of the current output module
  34. */
  35. protected string $defaultMode;
  36. /**
  37. * the current scaling for a QR pixel
  38. *
  39. * @see \chillerlan\QRCode\QROptions::$scale
  40. */
  41. protected int $scale;
  42. /**
  43. * the side length of the QR image (modules * scale)
  44. */
  45. protected int $length;
  46. /**
  47. * an (optional) array of color values for the several QR matrix parts
  48. */
  49. protected array $moduleValues;
  50. /**
  51. * the (filled) data matrix object
  52. */
  53. protected QRMatrix $matrix;
  54. /**
  55. * @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\QRCode\QROptions
  56. */
  57. protected SettingsContainerInterface $options;
  58. /**
  59. * QROutputAbstract constructor.
  60. */
  61. public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
  62. $this->options = $options;
  63. $this->matrix = $matrix;
  64. $this->moduleCount = $this->matrix->size();
  65. $this->scale = $this->options->scale;
  66. $this->length = $this->moduleCount * $this->scale;
  67. $class = get_called_class();
  68. if(isset(QRCode::OUTPUT_MODES[$class]) && in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){
  69. $this->outputMode = $this->options->outputType;
  70. }
  71. $this->setModuleValues();
  72. }
  73. /**
  74. * Sets the initial module values (clean-up & defaults)
  75. */
  76. abstract protected function setModuleValues():void;
  77. /**
  78. * Returns a base64 data URI for the given string and mime type
  79. */
  80. protected function base64encode(string $data, string $mime):string{
  81. return sprintf('data:%s;base64,%s', $mime, base64_encode($data));
  82. }
  83. /**
  84. * saves the qr data to a file
  85. *
  86. * @see file_put_contents()
  87. * @see \chillerlan\QRCode\QROptions::cachefile
  88. *
  89. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  90. */
  91. protected function saveToFile(string $data, string $file):bool{
  92. if(!is_writable(dirname($file))){
  93. throw new QRCodeOutputException(sprintf('Could not write data to cache file: %s', $file));
  94. }
  95. return (bool)file_put_contents($file, $data);
  96. }
  97. /**
  98. * @inheritDoc
  99. */
  100. public function dump(string $file = null){
  101. $file ??= $this->options->cachefile;
  102. // call the built-in output method with the optional file path as parameter
  103. // to make the called method aware if a cache file was given
  104. $data = call_user_func_array([$this, $this->outputMode ?? $this->defaultMode], [$file]);
  105. if($file !== null){
  106. $this->saveToFile($data, $file);
  107. }
  108. return $data;
  109. }
  110. }