DebugWriter.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * (c) Jeroen van den Enden <info@endroid.nl>
  5. *
  6. * This source file is subject to the MIT license that is bundled
  7. * with this source code in the file LICENSE.
  8. */
  9. namespace Endroid\QrCode\Writer;
  10. use Endroid\QrCode\QrCodeInterface;
  11. use Exception;
  12. use ReflectionClass;
  13. class DebugWriter extends AbstractWriter
  14. {
  15. public function writeString(QrCodeInterface $qrCode): string
  16. {
  17. $data = [];
  18. $skip = ['getData'];
  19. $reflectionClass = new ReflectionClass($qrCode);
  20. foreach ($reflectionClass->getMethods() as $method) {
  21. $methodName = $method->getShortName();
  22. if (0 === strpos($methodName, 'get') && 0 == $method->getNumberOfParameters() && !in_array($methodName, $skip)) {
  23. $value = $qrCode->{$methodName}();
  24. if (is_array($value) && !is_object(current($value))) {
  25. $value = '['.implode(', ', $value).']';
  26. } elseif (is_bool($value)) {
  27. $value = $value ? 'true' : 'false';
  28. } elseif (is_string($value)) {
  29. $value = '"'.$value.'"';
  30. } elseif (is_null($value)) {
  31. $value = 'null';
  32. }
  33. try {
  34. $data[] = $methodName.': '.$value;
  35. } catch (Exception $exception) {
  36. }
  37. }
  38. }
  39. $string = implode(" \n", $data);
  40. return $string;
  41. }
  42. public static function getContentType(): string
  43. {
  44. return 'text/plain';
  45. }
  46. public function getName(): string
  47. {
  48. return 'debug';
  49. }
  50. }