You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
3.0 KiB

3 years ago
  1. # sebastian/exporter
  2. [![CI Status](https://github.com/sebastianbergmann/exporter/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/exporter/actions)
  3. [![Type Coverage](https://shepherd.dev/github/sebastianbergmann/exporter/coverage.svg)](https://shepherd.dev/github/sebastianbergmann/exporter)
  4. This component provides the functionality to export PHP variables for visualization.
  5. ## Installation
  6. You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
  7. ```
  8. composer require sebastian/exporter
  9. ```
  10. If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
  11. ```
  12. composer require --dev sebastian/exporter
  13. ```
  14. ## Usage
  15. Exporting:
  16. ```php
  17. <?php
  18. use SebastianBergmann\Exporter\Exporter;
  19. $exporter = new Exporter;
  20. /*
  21. Exception Object &0000000078de0f0d000000002003a261 (
  22. 'message' => ''
  23. 'string' => ''
  24. 'code' => 0
  25. 'file' => '/home/sebastianbergmann/test.php'
  26. 'line' => 34
  27. 'previous' => null
  28. )
  29. */
  30. print $exporter->export(new Exception);
  31. ```
  32. ## Data Types
  33. Exporting simple types:
  34. ```php
  35. <?php
  36. use SebastianBergmann\Exporter\Exporter;
  37. $exporter = new Exporter;
  38. // 46
  39. print $exporter->export(46);
  40. // 4.0
  41. print $exporter->export(4.0);
  42. // 'hello, world!'
  43. print $exporter->export('hello, world!');
  44. // false
  45. print $exporter->export(false);
  46. // NAN
  47. print $exporter->export(acos(8));
  48. // -INF
  49. print $exporter->export(log(0));
  50. // null
  51. print $exporter->export(null);
  52. // resource(13) of type (stream)
  53. print $exporter->export(fopen('php://stderr', 'w'));
  54. // Binary String: 0x000102030405
  55. print $exporter->export(chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5));
  56. ```
  57. Exporting complex types:
  58. ```php
  59. <?php
  60. use SebastianBergmann\Exporter\Exporter;
  61. $exporter = new Exporter;
  62. /*
  63. Array &0 (
  64. 0 => Array &1 (
  65. 0 => 1
  66. 1 => 2
  67. 2 => 3
  68. )
  69. 1 => Array &2 (
  70. 0 => ''
  71. 1 => 0
  72. 2 => false
  73. )
  74. )
  75. */
  76. print $exporter->export(array(array(1,2,3), array("",0,FALSE)));
  77. /*
  78. Array &0 (
  79. 'self' => Array &1 (
  80. 'self' => Array &1
  81. )
  82. )
  83. */
  84. $array = array();
  85. $array['self'] = &$array;
  86. print $exporter->export($array);
  87. /*
  88. stdClass Object &0000000003a66dcc0000000025e723e2 (
  89. 'self' => stdClass Object &0000000003a66dcc0000000025e723e2
  90. )
  91. */
  92. $obj = new stdClass();
  93. $obj->self = $obj;
  94. print $exporter->export($obj);
  95. ```
  96. Compact exports:
  97. ```php
  98. <?php
  99. use SebastianBergmann\Exporter\Exporter;
  100. $exporter = new Exporter;
  101. // Array ()
  102. print $exporter->shortenedExport(array());
  103. // Array (...)
  104. print $exporter->shortenedExport(array(1,2,3,4,5));
  105. // stdClass Object ()
  106. print $exporter->shortenedExport(new stdClass);
  107. // Exception Object (...)
  108. print $exporter->shortenedExport(new Exception);
  109. // this\nis\na\nsuper\nlong\nstring\nt...\nspace
  110. print $exporter->shortenedExport(
  111. <<<LONG_STRING
  112. this
  113. is
  114. a
  115. super
  116. long
  117. string
  118. that
  119. wraps
  120. a
  121. lot
  122. and
  123. eats
  124. up
  125. a
  126. lot
  127. of
  128. space
  129. LONG_STRING
  130. );
  131. ```