Completed
Push — master ( c9cb42...6c1a95 )
by Asmir
18s queued 15s
created

shouldSkipClass()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 5
nop 2
dl 0
loc 21
ccs 6
cts 6
cp 1
crap 5
rs 9.5555
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Exclusion;
6
7
use JMS\Serializer\Context;
8
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
9
use JMS\Serializer\Expression\Expression;
10
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
11
use JMS\Serializer\Metadata\ClassMetadata;
12
use JMS\Serializer\Metadata\PropertyMetadata;
13
use JMS\Serializer\SerializationContext;
14
15
/**
16
 * Exposes an exclusion strategy based on the Symfony's expression language.
17
 * This is not a standard exclusion strategy and can not be used in user applications.
18
 *
19
 * @internal
20
 *
21
 * @author Asmir Mustafic <[email protected]>
22
 */
23
final class ExpressionLanguageExclusionStrategy
24
{
25
    /**
26
     * @var ExpressionEvaluatorInterface
27 27
     */
28
    private $expressionEvaluator;
29 27
30 27
    public function __construct(ExpressionEvaluatorInterface $expressionEvaluator)
31
    {
32
        $this->expressionEvaluator = $expressionEvaluator;
33
    }
34
35 27
36
    /**
37 27
     * {@inheritDoc}
38 26
     */
39
    public function shouldSkipClass(ClassMetadata $class, Context $navigatorContext): bool
40
    {
41
        if (null === $class->excludeIf) {
42 22
            return false;
43 22
        }
44
45 22
        $variables = [
46 22
            'context' => $navigatorContext,
47
            'class_metadata' => $class,
48 1
        ];
49
        if ($navigatorContext instanceof SerializationContext) {
50
            $variables['object'] = $navigatorContext->getObject();
51 22
        } else {
52
            $variables['object'] = null;
53
        }
54
55
        if (($class->excludeIf instanceof Expression) && ($this->expressionEvaluator instanceof CompilableExpressionEvaluatorInterface)) {
56
            return $this->expressionEvaluator->evaluateParsed($class->excludeIf, $variables);
57
        }
58
59
        return $this->expressionEvaluator->evaluate($class->excludeIf, $variables);
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext): bool
66
    {
67
        if (null === $property->excludeIf) {
68
            return false;
69
        }
70
71
        $variables = [
72
            'context' => $navigatorContext,
73
            'property_metadata' => $property,
74
        ];
75
        if ($navigatorContext instanceof SerializationContext) {
76
            $variables['object'] = $navigatorContext->getObject();
77
        } else {
78
            $variables['object'] = null;
79
        }
80
81
        if (($property->excludeIf instanceof Expression) && ($this->expressionEvaluator instanceof CompilableExpressionEvaluatorInterface)) {
82
            return $this->expressionEvaluator->evaluateParsed($property->excludeIf, $variables);
83
        }
84
85
        return $this->expressionEvaluator->evaluate($property->excludeIf, $variables);
86
    }
87
}
88