Completed
Push — master ( a8cf27...ed6ab4 )
by Eric
02:36
created

MaxDepthExclusionStrategy::skip()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
c 0
b 0
f 0
rs 8.439
cc 6
eloc 16
nc 9
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Exclusion;
13
14
use Ivory\Serializer\Context\ContextInterface;
15
use Ivory\Serializer\Mapping\ClassMetadataInterface;
16
use Ivory\Serializer\Mapping\PropertyMetadataInterface;
17
use Ivory\Serializer\Mapping\TypeMetadataInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class MaxDepthExclusionStrategy extends ExclusionStrategy
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function skipClass(ClassMetadataInterface $class, ContextInterface $context)
28
    {
29
        return $this->skip($context);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function skipType(TypeMetadataInterface $type, ContextInterface $context)
36
    {
37
        return $this->skip($context);
38
    }
39
40
    /**
41
     * @param ContextInterface $context
42
     *
43
     * @return bool
44
     */
45
    private function skip(ContextInterface $context)
46
    {
47
        $metadataStack = $context->getMetadataStack();
48
        $stackDepth = count($context->getDataStack());
49
        $depth = 0;
50
51
        for ($i = $metadataStack->count() - 1; $i > 0; --$i) {
52
            $metadata = $metadataStack[$i];
53
54
            if ($metadata instanceof TypeMetadataInterface) {
55
                ++$depth;
56
            }
57
58
            if (!$metadata instanceof PropertyMetadataInterface) {
59
                continue;
60
            }
61
62
            ++$depth;
63
64
            if (!$metadata->hasMaxDepth()) {
65
                continue;
66
            }
67
68
            if ($stackDepth - $depth > $metadata->getMaxDepth()) {
69
                return true;
70
            }
71
        }
72
73
        return false;
74
    }
75
}
76