PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sebastian/exporter/src/Exporter.php

https://gitlab.com/pthapa81/MeroSaaman-1.0
PHP | 302 lines | 168 code | 46 blank | 88 comment | 30 complexity | fcd5be8933475710881dd161b0227ddd MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Exporter package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Exporter;
  11. use SebastianBergmann\RecursionContext\Context;
  12. /**
  13. * A nifty utility for visualizing PHP variables.
  14. *
  15. * <code>
  16. * <?php
  17. * use SebastianBergmann\Exporter\Exporter;
  18. *
  19. * $exporter = new Exporter;
  20. * print $exporter->export(new Exception);
  21. * </code>
  22. *
  23. * @package Exporter
  24. * @author Sebastian Bergmann <sebastian@phpunit.de>
  25. * @copyright Sebastian Bergmann <sebastian@phpunit.de>
  26. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  27. * @link https://github.com/sebastianbergmann/exporter
  28. */
  29. class Exporter
  30. {
  31. /**
  32. * Exports a value as a string
  33. *
  34. * The output of this method is similar to the output of print_r(), but
  35. * improved in various aspects:
  36. *
  37. * - NULL is rendered as "null" (instead of "")
  38. * - TRUE is rendered as "true" (instead of "1")
  39. * - FALSE is rendered as "false" (instead of "")
  40. * - Strings are always quoted with single quotes
  41. * - Carriage returns and newlines are normalized to \n
  42. * - Recursion and repeated rendering is treated properly
  43. *
  44. * @param mixed $value
  45. * @param integer $indentation The indentation level of the 2nd+ line
  46. * @return string
  47. */
  48. public function export($value, $indentation = 0)
  49. {
  50. return $this->recursiveExport($value, $indentation);
  51. }
  52. /**
  53. * @param mixed $data
  54. * @param Context $context
  55. * @return string
  56. */
  57. public function shortenedRecursiveExport(&$data, Context $context = null)
  58. {
  59. $result = array();
  60. $exporter = new Exporter();
  61. if (!$context) {
  62. $context = new Context;
  63. }
  64. $context->add($data);
  65. foreach ($data as $key => $value) {
  66. if (is_array($value)) {
  67. if ($context->contains($data[$key]) !== false) {
  68. $result[] = '*RECURSION*';
  69. }
  70. else {
  71. $result[] = sprintf(
  72. 'array(%s)',
  73. $this->shortenedRecursiveExport($data[$key], $context)
  74. );
  75. }
  76. }
  77. else {
  78. $result[] = $exporter->shortenedExport($value);
  79. }
  80. }
  81. return join(', ', $result);
  82. }
  83. /**
  84. * Exports a value into a single-line string
  85. *
  86. * The output of this method is similar to the output of
  87. * SebastianBergmann\Exporter\Exporter::export. This method guarantees
  88. * thought that the result contains now newlines.
  89. *
  90. * Newlines are replaced by the visible string '\n'. Contents of arrays
  91. * and objects (if any) are replaced by '...'.
  92. *
  93. * @param mixed $value
  94. * @return string
  95. * @see SebastianBergmann\Exporter\Exporter::export
  96. */
  97. public function shortenedExport($value)
  98. {
  99. if (is_string($value)) {
  100. $string = $this->export($value);
  101. if (strlen($string) > 40) {
  102. $string = substr($string, 0, 30) . '...' . substr($string, -7);
  103. }
  104. return str_replace("\n", '\n', $string);
  105. }
  106. if (is_object($value)) {
  107. return sprintf(
  108. '%s Object (%s)',
  109. get_class($value),
  110. count($this->toArray($value)) > 0 ? '...' : ''
  111. );
  112. }
  113. if (is_array($value)) {
  114. return sprintf(
  115. 'Array (%s)',
  116. count($value) > 0 ? '...' : ''
  117. );
  118. }
  119. return $this->export($value);
  120. }
  121. /**
  122. * Converts an object to an array containing all of its private, protected
  123. * and public properties.
  124. *
  125. * @param mixed $value
  126. * @return array
  127. */
  128. public function toArray($value)
  129. {
  130. if (!is_object($value)) {
  131. return (array)$value;
  132. }
  133. $array = array();
  134. foreach ((array)$value as $key => $val) {
  135. // properties are transformed to keys in the following way:
  136. // private $property => "\0Classname\0property"
  137. // protected $property => "\0*\0property"
  138. // public $property => "property"
  139. if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) {
  140. $key = $matches[1];
  141. }
  142. // See https://github.com/php/php-src/commit/5721132
  143. if ($key === "\0gcdata") {
  144. continue;
  145. }
  146. $array[$key] = $val;
  147. }
  148. // Some internal classes like SplObjectStorage don't work with the
  149. // above (fast) mechanism nor with reflection in Zend.
  150. // Format the output similarly to print_r() in this case
  151. if ($value instanceof \SplObjectStorage) {
  152. // However, the fast method does work in HHVM, and exposes the
  153. // internal implementation. Hide it again.
  154. if (property_exists('\SplObjectStorage', '__storage')) {
  155. unset($array['__storage']);
  156. } elseif (property_exists('\SplObjectStorage', 'storage')) {
  157. unset($array['storage']);
  158. }
  159. if (property_exists('\SplObjectStorage', '__key')) {
  160. unset($array['__key']);
  161. }
  162. foreach ($value as $key => $val) {
  163. $array[spl_object_hash($val)] = array(
  164. 'obj' => $val,
  165. 'inf' => $value->getInfo(),
  166. );
  167. }
  168. }
  169. return $array;
  170. }
  171. /**
  172. * Recursive implementation of export
  173. *
  174. * @param mixed $value The value to export
  175. * @param integer $indentation The indentation level of the 2nd+ line
  176. * @param \SebastianBergmann\RecursionContext\Context $processed Previously processed objects
  177. * @return string
  178. * @see SebastianBergmann\Exporter\Exporter::export
  179. */
  180. protected function recursiveExport(&$value, $indentation, $processed = null)
  181. {
  182. if ($value === null) {
  183. return 'null';
  184. }
  185. if ($value === true) {
  186. return 'true';
  187. }
  188. if ($value === false) {
  189. return 'false';
  190. }
  191. if (is_float($value) && floatval(intval($value)) === $value) {
  192. return "$value.0";
  193. }
  194. if (is_resource($value)) {
  195. return sprintf(
  196. 'resource(%d) of type (%s)',
  197. $value,
  198. get_resource_type($value)
  199. );
  200. }
  201. if (is_string($value)) {
  202. // Match for most non printable chars somewhat taking multibyte chars into account
  203. if (preg_match('/[^\x09-\x0d\x20-\xff]/', $value)) {
  204. return 'Binary String: 0x' . bin2hex($value);
  205. }
  206. return "'" .
  207. str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) .
  208. "'";
  209. }
  210. $whitespace = str_repeat(' ', 4 * $indentation);
  211. if (!$processed) {
  212. $processed = new Context;
  213. }
  214. if (is_array($value)) {
  215. if (($key = $processed->contains($value)) !== false) {
  216. return 'Array &' . $key;
  217. }
  218. $key = $processed->add($value);
  219. $values = '';
  220. if (count($value) > 0) {
  221. foreach ($value as $k => $v) {
  222. $values .= sprintf(
  223. '%s %s => %s' . "\n",
  224. $whitespace,
  225. $this->recursiveExport($k, $indentation),
  226. $this->recursiveExport($value[$k], $indentation + 1, $processed)
  227. );
  228. }
  229. $values = "\n" . $values . $whitespace;
  230. }
  231. return sprintf('Array &%s (%s)', $key, $values);
  232. }
  233. if (is_object($value)) {
  234. $class = get_class($value);
  235. if ($hash = $processed->contains($value)) {
  236. return sprintf('%s Object &%s', $class, $hash);
  237. }
  238. $hash = $processed->add($value);
  239. $values = '';
  240. $array = $this->toArray($value);
  241. if (count($array) > 0) {
  242. foreach ($array as $k => $v) {
  243. $values .= sprintf(
  244. '%s %s => %s' . "\n",
  245. $whitespace,
  246. $this->recursiveExport($k, $indentation),
  247. $this->recursiveExport($v, $indentation + 1, $processed)
  248. );
  249. }
  250. $values = "\n" . $values . $whitespace;
  251. }
  252. return sprintf('%s Object &%s (%s)', $class, $hash, $values);
  253. }
  254. return var_export($value, true);
  255. }
  256. }