Passed
Pull Request — master (#50)
by Alex
03:50
created

ValueHelpers::IsSerializedAsElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 22/07/20
6
 * Time: 8:21 PM
7
 */
8
9
namespace AlgoWeb\ODataMetadata\Helpers;
10
11
use AlgoWeb\ODataMetadata\CsdlConstants;
12
use AlgoWeb\ODataMetadata\Edm\Validation\Internal\ValidationHelper;
13
use AlgoWeb\ODataMetadata\EdmConstants;
14
use AlgoWeb\ODataMetadata\Exception\InvalidOperationException;
15
use AlgoWeb\ODataMetadata\Interfaces\IEdmElement;
16
use AlgoWeb\ODataMetadata\Interfaces\IModel;
17
18
trait ValueHelpers
19
{
20
    /**
21
     * Sets an annotation indicating if the value should be serialized as an element.
22
     * @param IModel $model Model containing the value.
23
     * @param bool $isSerializedAsElement Value indicating if the value should be serialized as an element.
24
     */
25
    public function SetIsSerializedAsElement(IModel $model, bool $isSerializedAsElement): void
26
    {
27
        /** @var IEdmElement $self */
28
        $self = $this;
29
        assert($self instanceof IEdmElement);
30
        if ($isSerializedAsElement) {
31
            $error = null;
32
            if (!ValidationHelper::ValidateValueCanBeWrittenAsXmlElementAnnotation(
33
                $self,
34
                null,
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $annotationNamespace of AlgoWeb\ODataMetadata\Ed...sXmlElementAnnotation(). ( Ignorable by Annotation )

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

34
                /** @scrutinizer ignore-type */ null,
Loading history...
35
                null,
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $annotationName of AlgoWeb\ODataMetadata\Ed...sXmlElementAnnotation(). ( Ignorable by Annotation )

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

35
                /** @scrutinizer ignore-type */ null,
Loading history...
36
                $error
0 ignored issues
show
Bug introduced by
$error of type null is incompatible with the type AlgoWeb\ODataMetadata\Edm\Validation\EdmError expected by parameter $error of AlgoWeb\ODataMetadata\Ed...sXmlElementAnnotation(). ( Ignorable by Annotation )

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

36
                /** @scrutinizer ignore-type */ $error
Loading history...
37
            )
38
            ) {
39
                throw new InvalidOperationException(strval($error));
40
            }
41
        }
42
43
        $model->SetAnnotationValue(
44
            $self,
45
            EdmConstants::InternalUri,
46
            CsdlConstants::IsSerializedAsElementAnnotation,
47
            $isSerializedAsElement
48
        );
49
    }
50
51
    /**
52
     * Gets an annotation indicating if the value should be serialized as an element.
53
     * @param IModel $model  Model containing the value.
54
     * @return bool Value indicating if the string should be serialized as an element.
55
     */
56
    public function IsSerializedAsElement(IModel $model): bool
57
    {
58
        $self = $this;
59
        assert($self instanceof IEdmElement);
60
        $value = $model->GetAnnotationValue(
61
            'boolean',
62
            $self,
63
            EdmConstants::InternalUri,
64
            CsdlConstants::IsSerializedAsElementAnnotation
65
        );
66
        return is_bool($value) ? $value : false;
67
    }
68
}
69