custom_output.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * custom output example
  4. *
  5. * @created 24.12.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpIllegalPsrClassPathInspection
  11. */
  12. use chillerlan\QRCode\{QRCode, QROptions};
  13. use chillerlan\QRCode\Output\QROutputAbstract;
  14. require_once __DIR__.'/../vendor/autoload.php';
  15. /*
  16. * Class definition
  17. */
  18. class MyCustomOutput extends QROutputAbstract{
  19. /**
  20. * @inheritDoc
  21. */
  22. public static function moduleValueIsValid(mixed $value):bool{
  23. // TODO: Implement moduleValueIsValid() method. (interface)
  24. return false;
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. protected function prepareModuleValue(mixed $value):mixed{
  30. // TODO: Implement prepareModuleValue() method. (abstract)
  31. return null;
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. protected function getDefaultModuleValue(bool $isDark):mixed{
  37. // TODO: Implement getDefaultModuleValue() method. (abstract)
  38. return null;
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. public function dump(string $file = null):string{
  44. $output = '';
  45. for($y = 0; $y < $this->moduleCount; $y++){
  46. for($x = 0; $x < $this->moduleCount; $x++){
  47. $output .= (int)$this->matrix->check($x, $y);
  48. }
  49. $output .= $this->options->eol;
  50. }
  51. return $output;
  52. }
  53. }
  54. /*
  55. * Runtime
  56. */
  57. $options = new QROptions;
  58. $options->version = 5;
  59. $options->eccLevel = 'L';
  60. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  61. // invoke the QROutputInterface manually
  62. // please note that an QROutputInterface invoked this way might become unusable after calling dump().
  63. // the clean way would be to extend the QRCode class to ensure a new QROutputInterface instance on each call to render().
  64. $qrcode = new QRCode($options);
  65. $qrcode->addByteSegment($data);
  66. $qrOutputInterface = new MyCustomOutput($options, $qrcode->getQRMatrix());
  67. var_dump($qrOutputInterface->dump());
  68. // or just via the options
  69. $options->outputInterface = MyCustomOutput::class;
  70. var_dump((new QRCode($options))->render($data));
  71. exit;