custom_output.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. declare(strict_types=1);
  13. use chillerlan\QRCode\{QRCode, QROptions};
  14. use chillerlan\QRCode\Output\QROutputAbstract;
  15. require_once __DIR__.'/../vendor/autoload.php';
  16. /*
  17. * Class definition
  18. */
  19. class MyCustomOutput extends QROutputAbstract{
  20. public static function moduleValueIsValid(mixed $value):bool{
  21. // TODO: Implement moduleValueIsValid() method. (interface)
  22. return false;
  23. }
  24. protected function prepareModuleValue(mixed $value):mixed{
  25. // TODO: Implement prepareModuleValue() method. (abstract)
  26. return null;
  27. }
  28. protected function getDefaultModuleValue(bool $isDark):mixed{
  29. // TODO: Implement getDefaultModuleValue() method. (abstract)
  30. return null;
  31. }
  32. public function dump(string|null $file = null):string{
  33. $output = '';
  34. for($y = 0; $y < $this->moduleCount; $y++){
  35. for($x = 0; $x < $this->moduleCount; $x++){
  36. $output .= (int)$this->matrix->check($x, $y);
  37. }
  38. $output .= $this->options->eol;
  39. }
  40. return $output;
  41. }
  42. }
  43. /*
  44. * Runtime
  45. */
  46. $options = new QROptions;
  47. $options->version = 5;
  48. /** @phan-suppress-next-line PhanTypeMismatchPropertyProbablyReal */
  49. $options->eccLevel = 'L'; // can be assigned as string
  50. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  51. // invoke the QROutputInterface manually
  52. // please note that an QROutputInterface invoked this way might become unusable after calling dump().
  53. // the clean way would be to extend the QRCode class to ensure a new QROutputInterface instance on each call to render().
  54. $qrcode = new QRCode($options);
  55. $qrcode->addByteSegment($data);
  56. $qrOutputInterface = new MyCustomOutput($options, $qrcode->getQRMatrix());
  57. var_dump($qrOutputInterface->dump());
  58. // or just via the options
  59. $options->outputInterface = MyCustomOutput::class;
  60. var_dump((new QRCode($options))->render($data));
  61. exit;