diff --git a/lib/Raven/Serializer.php b/lib/Raven/Serializer.php index 79741c075..e58216aa0 100644 --- a/lib/Raven/Serializer.php +++ b/lib/Raven/Serializer.php @@ -52,6 +52,13 @@ class Raven_Serializer */ protected $message_limit = Raven_Client::MESSAGE_LIMIT; + /** + * The default max depth. + * + * @var int + */ + protected $default_max_depth = 3; + /** * @param null|string $mb_detect_order * @param null|int $message_limit @@ -71,14 +78,17 @@ public function __construct($mb_detect_order = null, $message_limit = null) * Serialize an object (recursively) into something safe for data * sanitization and encoding. * - * @param mixed $value - * @param int $max_depth - * @param int $_depth + * @param mixed $value + * @param int|null $max_depth + * @param int $_depth * @return string|bool|double|int|null|object|array */ - public function serialize($value, $max_depth = 3, $_depth = 0) + public function serialize($value, $max_depth = null, $_depth = 0) { $className = is_object($value) ? get_class($value) : null; + if (is_null($max_depth)) { + $max_depth = $this->getDefaultMaxDepth(); + } $toArray = is_array($value) || $className === 'stdClass'; if ($toArray && $_depth < $max_depth) { $new = array(); @@ -175,4 +185,20 @@ public function setMessageLimit($message_limit) { $this->message_limit = (int)$message_limit; } + + /** + * @return int + */ + public function getDefaultMaxDepth() + { + return $this->default_max_depth; + } + + /** + * @param int $max_depth + */ + public function setDefaultMaxDepth($max_depth) + { + $this->default_max_depth = (int)$max_depth; + } } diff --git a/test/Raven/Tests/SerializerTest.php b/test/Raven/Tests/SerializerTest.php index 0562e5b36..92d21a6f5 100644 --- a/test/Raven/Tests/SerializerTest.php +++ b/test/Raven/Tests/SerializerTest.php @@ -201,4 +201,49 @@ public function testClippingUTF8Characters() $this->assertEquals(JSON_ERROR_NONE, json_last_error()); } + + /** + * @covers Raven_Serializer::getDefaultMaxDepth + * @covers Raven_Serializer::setDefaultMaxDepth + */ + public function testChangeDefaultMaxDepth() + { + $serializer = new Raven_Serializer(); + $input = array( + 1 => array( + 2 => array( + 3 => array( + 4 => array( + 5 => 6 + ) + ) + ) + ) + ); + $expectedDefaultResult = array( + 1 => array( + 2 => array( + 3 => 'Array of length 1' + ) + ) + ); + $this->assertSame( + $expectedDefaultResult, + $serializer->serialize($input) + ); + $expectedChangedResult = array( + 1 => array( + 2 => array( + 3 => array( + 4 => 'Array of length 1' + ) + ) + ) + ); + $serializer->setDefaultMaxDepth(4); + $this->assertSame( + $expectedChangedResult, + $serializer->serialize($input) + ); + } }