QRString.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Class QRString
  4. *
  5. * @created 05.12.2015
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2015 Smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpUnusedParameterInspection
  11. * @noinspection PhpComposerExtensionStubsInspection
  12. */
  13. namespace chillerlan\QRCode\Output;
  14. use function implode, is_string, json_encode;
  15. /**
  16. * Converts the matrix data into string types
  17. */
  18. class QRString extends QROutputAbstract{
  19. /**
  20. * @inheritDoc
  21. */
  22. protected function moduleValueIsValid($value):bool{
  23. return is_string($value);
  24. }
  25. /**
  26. * @inheritDoc
  27. */
  28. protected function getModuleValue($value):string{
  29. return $value;
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. protected function getDefaultModuleValue(bool $isDark):string{
  35. return ($isDark) ? $this->options->textDark : $this->options->textLight;
  36. }
  37. /**
  38. * @inheritDoc
  39. */
  40. public function dump(string $file = null):string{
  41. switch($this->options->outputType){
  42. case QROutputInterface::STRING_TEXT:
  43. $data = $this->text();
  44. break;
  45. case QROutputInterface::STRING_JSON:
  46. default:
  47. $data = $this->json();
  48. }
  49. $this->saveToFile($data, $file);
  50. return $data;
  51. }
  52. /**
  53. * string output
  54. */
  55. protected function text():string{
  56. $str = [];
  57. foreach($this->matrix->matrix() as $row){
  58. $r = [];
  59. foreach($row as $M_TYPE){
  60. $r[] = $this->moduleValues[$M_TYPE];
  61. }
  62. $str[] = implode('', $r);
  63. }
  64. return implode($this->options->eol, $str);
  65. }
  66. /**
  67. * JSON output
  68. */
  69. protected function json():string{
  70. return json_encode($this->matrix->matrix());
  71. }
  72. }