QROutputAbstract.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 call_user_func, dirname, file_put_contents, get_called_class, in_array, is_writable;
  16. /**
  17. * common output abstract
  18. */
  19. abstract class QROutputAbstract implements QROutputInterface{
  20. /**
  21. * @var int
  22. */
  23. protected $moduleCount;
  24. /**
  25. * @param \chillerlan\QRCode\Data\QRMatrix $matrix
  26. */
  27. protected $matrix;
  28. /**
  29. * @var \chillerlan\QRCode\QROptions
  30. */
  31. protected $options;
  32. /**
  33. * @var string
  34. */
  35. protected $outputMode;
  36. /**
  37. * @var string;
  38. */
  39. protected $defaultMode;
  40. /**
  41. * @var int
  42. */
  43. protected $scale;
  44. /**
  45. * @var int
  46. */
  47. protected $length;
  48. /**
  49. * @var array
  50. */
  51. protected $moduleValues;
  52. /**
  53. * QROutputAbstract constructor.
  54. *
  55. * @param \chillerlan\Settings\SettingsContainerInterface $options
  56. * @param \chillerlan\QRCode\Data\QRMatrix $matrix
  57. */
  58. public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
  59. $this->options = $options;
  60. $this->matrix = $matrix;
  61. $this->moduleCount = $this->matrix->size();
  62. $this->scale = $this->options->scale;
  63. $this->length = $this->moduleCount * $this->scale;
  64. $class = get_called_class();
  65. if(\array_key_exists($class, QRCode::OUTPUT_MODES) && \in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){
  66. $this->outputMode = $this->options->outputType;
  67. }
  68. $this->setModuleValues();
  69. }
  70. /**
  71. * Sets the initial module values (clean-up & defaults)
  72. *
  73. * @return void
  74. */
  75. abstract protected function setModuleValues():void;
  76. /**
  77. * @see file_put_contents()
  78. *
  79. * @param string $data
  80. * @param string $file
  81. *
  82. * @return bool
  83. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  84. */
  85. protected function saveToFile(string $data, string $file):bool{
  86. if(!is_writable(dirname($file))){
  87. throw new QRCodeOutputException('Could not write data to cache file: '.$file);
  88. }
  89. return (bool)file_put_contents($file, $data);
  90. }
  91. /**
  92. * @param string|null $file
  93. *
  94. * @return string|mixed
  95. */
  96. public function dump(string $file = null){
  97. $data = call_user_func([$this, $this->outputMode ?? $this->defaultMode]);
  98. $file = $file ?? $this->options->cachefile;
  99. if($file !== null){
  100. $this->saveToFile($data, $file);
  101. }
  102. return $data;
  103. }
  104. }