Serializer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * Copyright 2012 Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * This helper is based on code from Facebook's Phabricator project
  19. *
  20. * https://github.com/facebook/phabricator
  21. *
  22. * Specifically, it is an adaptation of the PhutilReadableSerializer class.
  23. *
  24. * @package raven
  25. */
  26. class Raven_Serializer
  27. {
  28. /*
  29. * The default mb detect order
  30. *
  31. * @see http://php.net/manual/en/function.mb-detect-encoding.php
  32. */
  33. const DEFAULT_MB_DETECT_ORDER = 'auto';
  34. /*
  35. * Suggested detect order for western countries
  36. */
  37. const WESTERN_MB_DETECT_ORDER = 'UTF-8, ASCII, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16, Windows-1251, Windows-1252, Windows-1254';
  38. /**
  39. * This is the default mb detect order for the detection of encoding
  40. *
  41. * @var string
  42. */
  43. protected $mb_detect_order = self::DEFAULT_MB_DETECT_ORDER;
  44. /**
  45. * @param null|string $mb_detect_order
  46. */
  47. public function __construct($mb_detect_order = null)
  48. {
  49. if ($mb_detect_order != null) {
  50. $this->mb_detect_order = $mb_detect_order;
  51. }
  52. }
  53. /**
  54. * Serialize an object (recursively) into something safe for data
  55. * sanitization and encoding.
  56. *
  57. * @param mixed $value
  58. * @param int $max_depth
  59. * @param int $_depth
  60. * @return string|bool|double|int|null|object|array
  61. */
  62. public function serialize($value, $max_depth = 3, $_depth = 0)
  63. {
  64. $className = is_object($value) ? get_class($value) : null;
  65. $toArray = is_array($value) || $className === 'stdClass';
  66. if ($toArray && $_depth < $max_depth) {
  67. $new = array();
  68. foreach ($value as $k => $v) {
  69. $new[$this->serializeValue($k)] = $this->serialize($v, $max_depth, $_depth + 1);
  70. }
  71. return $new;
  72. }
  73. return $this->serializeValue($value);
  74. }
  75. protected function serializeString($value)
  76. {
  77. $value = (string) $value;
  78. if (function_exists('mb_detect_encoding')
  79. && function_exists('mb_convert_encoding')
  80. ) {
  81. // we always guarantee this is coerced, even if we can't detect encoding
  82. if ($currentEncoding = mb_detect_encoding($value, $this->mb_detect_order)) {
  83. $value = mb_convert_encoding($value, 'UTF-8', $currentEncoding);
  84. } else {
  85. $value = mb_convert_encoding($value, 'UTF-8');
  86. }
  87. }
  88. if (strlen($value) > 1024) {
  89. $value = substr($value, 0, 1014) . ' {clipped}';
  90. }
  91. return $value;
  92. }
  93. /**
  94. * @param mixed $value
  95. * @return string|bool|double|int|null
  96. */
  97. protected function serializeValue($value)
  98. {
  99. if (is_null($value) || is_bool($value) || is_float($value) || is_integer($value)) {
  100. return $value;
  101. } elseif (is_object($value) || gettype($value) == 'object') {
  102. return 'Object '.get_class($value);
  103. } elseif (is_resource($value)) {
  104. return 'Resource '.get_resource_type($value);
  105. } elseif (is_array($value)) {
  106. return 'Array of length ' . count($value);
  107. } else {
  108. return $this->serializeString($value);
  109. }
  110. }
  111. /**
  112. * @return string
  113. * @codeCoverageIgnore
  114. */
  115. public function getMbDetectOrder()
  116. {
  117. return $this->mb_detect_order;
  118. }
  119. /**
  120. * @param string $mb_detect_order
  121. *
  122. * @return Raven_Serializer
  123. * @codeCoverageIgnore
  124. */
  125. public function setMbDetectOrder($mb_detect_order)
  126. {
  127. $this->mb_detect_order = $mb_detect_order;
  128. return $this;
  129. }
  130. }