QROutputAbstract.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\{
  14. Data\QRMatrix, QROptions
  15. };
  16. /**
  17. *
  18. */
  19. abstract class QROutputAbstract implements QROutputInterface{
  20. /**
  21. * @param \chillerlan\QRCode\Data\QRMatrix $matrix
  22. */
  23. protected $matrix;
  24. /**
  25. * @var int
  26. */
  27. protected $moduleCount;
  28. /**
  29. * @var object
  30. */
  31. protected $options;
  32. /**
  33. * @param \chillerlan\QRCode\QROptions $options
  34. * @param \chillerlan\QRCode\Data\QRMatrix $matrix
  35. *
  36. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  37. */
  38. public function __construct(QROptions $options, QRMatrix $matrix){
  39. $this->options = $options;
  40. $this->moduleCount = $matrix->size();
  41. if($this->moduleCount < 21){ // minimum QR modules @todo: quet zone
  42. throw new QRCodeOutputException('Invalid matrix!');
  43. }
  44. $this->matrix = $matrix;
  45. }
  46. /**
  47. * @param string $data
  48. *
  49. * @return bool
  50. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  51. */
  52. protected function saveToFile(string $data):bool {
  53. try{
  54. return (bool)file_put_contents($this->options->cachefile, $data);
  55. }
  56. catch(\Exception $e){
  57. throw new QRCodeOutputException('Could not write data to cache file: '.$e->getMessage());
  58. }
  59. }
  60. }