Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to changeable default_max_depth in Raven_Serializer #632

Merged
merged 3 commits into from
May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions lib/Raven/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
45 changes: 45 additions & 0 deletions test/Raven/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}