QRString.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Class QRString
  4. *
  5. * @filesource QRString.php
  6. * @created 05.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\QRCode;
  14. /**
  15. * Converts the matrix data into string types
  16. */
  17. class QRString extends QROutputAbstract{
  18. /**
  19. * @return string
  20. */
  21. public function dump():string{
  22. switch($this->options->outputType){
  23. case QRCode::OUTPUT_STRING_TEXT:
  24. return $this->toString();
  25. case QRCode::OUTPUT_STRING_JSON:
  26. default:
  27. return json_encode($this->matrix->matrix());
  28. }
  29. }
  30. /**
  31. * @return string
  32. */
  33. protected function toString():string{
  34. $str = '';
  35. foreach($this->matrix->matrix() as $row){
  36. foreach($row as $col){
  37. $col = $this->options->moduleValues[$col];
  38. // fallback
  39. if(is_bool($col) || !is_string($col)){
  40. $col = $col ? $this->options->textDark : $this->options->textLight;
  41. }
  42. $str .= $col;
  43. }
  44. $str .= $this->options->eol;
  45. }
  46. return $str;
  47. }
  48. }