Skip to content

Commit

Permalink
Changeable defaultMaxDepth in Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
howyi committed Apr 8, 2019
1 parent 2713970 commit c3a4c38
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/Serializer/AbstractSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ abstract class AbstractSerializer
*/
private $maxDepth;

/**
* The default max depth.
*
* @var int
*/
private $defaultMaxDepth = 3;

/**
* This is the default mb detect order for the detection of encoding.
*
Expand Down Expand Up @@ -78,12 +85,12 @@ abstract class AbstractSerializer
* AbstractSerializer constructor.
*
* @param Options $options The SDK configuration options
* @param int $maxDepth
* @param int|null $maxDepth
* @param string|null $mbDetectOrder
*/
public function __construct(Options $options, int $maxDepth = 3, ?string $mbDetectOrder = null)
public function __construct(Options $options, ?int $maxDepth = null, ?string $mbDetectOrder = null)
{
$this->maxDepth = $maxDepth;
$this->setMaxDepth($maxDepth);

if (null != $mbDetectOrder) {
$this->mbDetectOrder = $mbDetectOrder;
Expand All @@ -103,7 +110,7 @@ public function __construct(Options $options, int $maxDepth = 3, ?string $mbDete
*/
protected function serializeRecursively($value, int $_depth = 0)
{
if ($_depth >= $this->maxDepth) {
if ($_depth >= $this->getMaxDepth()) {
return $this->serializeValue($value);
}

Expand Down Expand Up @@ -139,7 +146,7 @@ protected function serializeRecursively($value, int $_depth = 0)
*/
protected function serializeObject($object, int $_depth = 0, array $hashes = [])
{
if ($_depth >= $this->maxDepth || \in_array(spl_object_hash($object), $hashes, true)) {
if ($_depth >= $this->getMaxDepth() || \in_array(spl_object_hash($object), $hashes, true)) {
return $this->serializeValue($object);
}

Expand Down Expand Up @@ -305,4 +312,36 @@ public function getSerializeAllObjects(): bool
{
return $this->serializeAllObjects;
}

/**
* @return int
*/
public function getMaxDepth(): int
{
return $this->maxDepth ?? $this->getDefaultMaxDepth();
}

/**
* @param int|null $maxDepth
*/
public function setMaxDepth(?int $maxDepth): void
{
$this->maxDepth = $maxDepth;
}

/**
* @return int
*/
public function getDefaultMaxDepth(): int
{
return $this->defaultMaxDepth;
}

/**
* @param int $maxDepth
*/
public function setDefaultMaxDepth(int $maxDepth): void
{
$this->defaultMaxDepth = (int) $maxDepth;
}
}
40 changes: 40 additions & 0 deletions tests/Serializer/AbstractSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,46 @@ public function testObjectInArraySerializeAll(): void
$this->assertSame(['foo' => ['key' => 'value']], $result);
}

public function testChangeDefaultMaxDepth(): void
{
$serializer = $this->createSerializer();
$serializer->setSerializeAllObjects(true);
$input = [
1 => [
2 => [
3 => [
4 => [
5 => 6
]
]
]
]
];
$expectedDefaultResult = [
1 => [
2 => [
3 => 'Array of length 1'
]
]
];

$result = $this->invokeSerialization($serializer, $input);
$this->assertSame($expectedDefaultResult, $result);

$expectedChangedResult = [
1 => [
2 => [
3 => [
4 => 'Array of length 1'
]
]
]
];
$serializer->setDefaultMaxDepth(4);
$result = $this->invokeSerialization($serializer, $input);
$this->assertSame($expectedChangedResult, $result);
}

/**
* @dataProvider serializeAllObjectsDataProvider
*/
Expand Down

0 comments on commit c3a4c38

Please sign in to comment.