Issues (126)

src/Helpers/VocabularyAnnotationHelpers.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\ODataMetadata\Helpers;
6
7
use AlgoWeb\ODataMetadata\CsdlConstants;
8
use AlgoWeb\ODataMetadata\EdmConstants;
9
use AlgoWeb\ODataMetadata\EdmUtil;
10
use AlgoWeb\ODataMetadata\Enums\EdmVocabularyAnnotationSerializationLocation;
11
use AlgoWeb\ODataMetadata\Interfaces\Annotations\IVocabularyAnnotation;
12
use AlgoWeb\ODataMetadata\Interfaces\IModel;
13
14
trait VocabularyAnnotationHelpers
15
{
16
    public function isInline(IModel $model): bool
17
    {
18
        /** @var IVocabularyAnnotation $annotation */
19
        $annotation = $this;
20
        return $annotation->getSerializationLocation($model) ==
21
               EdmVocabularyAnnotationSerializationLocation::Inline() || null === $annotation->targetString();
22
    }
23
24
    public function targetString(): string
25
    {
26
        /** @var IVocabularyAnnotation $annotation */
27
        $annotation = $this;
28
        EdmUtil::checkArgumentNull($annotation->getTarget(), 'annotation->getTarget');
29
        return EdmUtil::fullyQualifiedName($annotation->getTarget());
0 ignored issues
show
Bug Best Practice introduced by
The expression return AlgoWeb\ODataMeta...nnotation->getTarget()) could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
30
    }
31
32
    /**
33
     * Sets the location an annotation should be serialized in.
34
     *
35
     * @param IModel                                            $model    model containing the annotation
36
     * @param EdmVocabularyAnnotationSerializationLocation|null $location the location the annotation should appear
37
     */
38
    public function setSerializationLocation(
39
        IModel $model,
40
        ?EdmVocabularyAnnotationSerializationLocation $location
41
    ): void {
42
        /** @var IVocabularyAnnotation $annotation */
43
        $annotation = $this;
44
        $model->setAnnotationValue(
45
            $annotation,
46
            EdmConstants::InternalUri,
47
            CsdlConstants::AnnotationSerializationLocationAnnotation,
48
            (object)$location
49
        );
50
    }
51
52
    /**
53
     * Gets the location an annotation should be serialized in.
54
     *
55
     * @param  IModel                                            $model model containing the annotation
56
     * @return EdmVocabularyAnnotationSerializationLocation|null the location the annotation should be serialized at
57
     */
58
    public function getSerializationLocation(IModel $model): ?EdmVocabularyAnnotationSerializationLocation
59
    {
60
        /** @var IVocabularyAnnotation $annotation */
61
        $annotation = $this;
62
        $location   = $model->getAnnotationValue(
63
            EdmVocabularyAnnotationSerializationLocation::class,
64
            $annotation,
65
            EdmConstants::InternalUri,
66
            CsdlConstants::AnnotationSerializationLocationAnnotation
67
        );
68
        return $location instanceof EdmVocabularyAnnotationSerializationLocation ? $location : null;
69
    }
70
71
    /**
72
     * Sets the schema an annotation should appear in.
73
     *
74
     * @param IModel $model           model containing the annotation
75
     * @param string $schemaNamespace the schema the annotation belongs in
76
     */
77
    public function setSchemaNamespace(IModel $model, string $schemaNamespace): void
78
    {
79
        /** @var IVocabularyAnnotation $annotation */
80
        $annotation = $this;
81
        $model->setAnnotationValue(
82
            $annotation,
83
            EdmConstants::InternalUri,
84
            CsdlConstants::SchemaNamespaceAnnotation,
85
            $schemaNamespace
86
        );
87
    }
88
    /**
89
     * Gets the schema an annotation should be serialized in.
90
     *
91
     * @param  IModel $model model containing the annotation
92
     * @return string name of the schema the annotation belongs to
93
     */
94
    public function getSchemaNamespace(IModel $model): string
95
    {
96
        /** @var IVocabularyAnnotation $annotation */
97
        $annotation = $this;
98
        return $model->getAnnotationValue(
99
            'string',
100
            $annotation,
101
            EdmConstants::InternalUri,
102
            CsdlConstants::SchemaNamespaceAnnotation
103
        );
104
    }
105
}
106