Test Failed
Push — main ( f1b82a...d2d042 )
by Bingo
06:24
created

JsonQueryFilteringPropertyConverter::toObject()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 21
rs 9.9332
cc 4
nc 8
nop 2
1
<?php
2
3
namespace Jabe\Engine\Impl\Json;
4
5
use Jabe\Engine\Impl\{
6
    QueryEntityRelationCondition,
7
    QueryPropertyImpl
8
};
9
use Jabe\Engine\Impl\Util\JsonUtil;
10
11
class JsonQueryFilteringPropertyConverter extends JsonObjectConverter
12
{
13
    protected static $INSTANCE;// = new JsonQueryFilteringPropertyConverter();
14
15
    protected static $ARRAY_CONVERTER;// = new JsonArrayOfObjectsConverter<>(INSTANCE);
16
17
    public const BASE_PROPERTY = "baseField";
18
    public const COMPARISON_PROPERTY = "comparisonField";
19
    public const SCALAR_VALUE = "value";
20
21
    public static function instance(): JsonQueryFilteringPropertyConverter
22
    {
23
        if (self::$INSTANCE === null) {
24
            self::$INSTANCE = new JsonQueryFilteringPropertyConverter();
25
        }
26
        return self::$INSTANCE;
27
    }
28
29
    public static function arrayConverter(): JsonArrayOfObjectsConverter
30
    {
31
        if (self::$ARRAY_CONVERTER === null) {
32
            self::$ARRAY_CONVERTER = new JsonArrayOfObjectsConverter(self::instance());
33
        }
34
        return self::$ARRAY_CONVERTER;
35
    }
36
37
    public function toJsonObject(/*QueryEntityRelationCondition*/$filteringProperty, bool $isOrQueryActive = false): ?\stdClass
38
    {
39
        $jsonObject = JsonUtil::createObject();
40
41
        JsonUtil::addField($jsonObject, self::BASE_PROPERTY, $filteringProperty->getProperty()->getName());
42
43
        $comparisonProperty = $filteringProperty->getComparisonProperty();
44
        if ($comparisonProperty !== null) {
45
            JsonUtil::addField($jsonObject, self::COMPARISON_PROPERTY, $comparisonProperty->getName());
46
        }
47
48
        $scalarValue = $filteringProperty->getScalarValue();
49
        if ($scalarValue !== null) {
50
            JsonUtil::addFieldRawValue($jsonObject, self::SCALAR_VALUE, $scalarValue);
51
        }
52
53
        return $jsonObject;
54
    }
55
56
    public function toObject(\stdClass $jsonObject, bool $isOrQuery = false)
57
    {
58
        // this is limited in that it allows only String values;
59
        // that is sufficient for current use case with task filters
60
        // but could be extended by a data type in the future
61
        $scalarValue = null;
62
        if (property_exists($jsonObject, self::SCALAR_VALUE)) {
63
            $scalarValue = JsonUtil::getString($jsonObject, self::SCALAR_VALUE);
64
        }
65
66
        $baseProperty = null;
67
        if (property_exists($jsonObject, self::BASE_PROPERTY)) {
68
            $baseProperty = new QueryPropertyImpl(JsonUtil::getString($jsonObject, self::BASE_PROPERTY));
69
        }
70
71
        $comparisonProperty = null;
72
        if (property_exists($jsonObject, self::COMPARISON_PROPERTY)) {
73
            $comparisonProperty = new QueryPropertyImpl(JsonUtil::getString($jsonObject, self::COMPARISON_PROPERTY));
74
        }
75
76
        return new QueryEntityRelationCondition($baseProperty, $comparisonProperty, $scalarValue);
0 ignored issues
show
Bug introduced by
It seems like $baseProperty can also be of type null; however, parameter $queryProperty of Jabe\Engine\Impl\QueryEn...ondition::__construct() does only seem to accept Jabe\Engine\Query\QueryPropertyInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        return new QueryEntityRelationCondition(/** @scrutinizer ignore-type */ $baseProperty, $comparisonProperty, $scalarValue);
Loading history...
77
    }
78
}
79