Completed
Push — master ( 0c1c20...3b6341 )
by Christopher
01:35
created

PropertyValue::getChildElements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\ODataMetadata\MetadataV3\Edm\Annotations;
6
7
use AlgoWeb\ODataMetadata\MetadataV3\DomBase;
8
use AlgoWeb\ODataMetadata\MetadataV3\Edm\EdmBase;
9
use AlgoWeb\ODataMetadata\MetadataV3\Edm\Expressions;
10
use AlgoWeb\ODataMetadata\Writer\AttributeContainer;
11
12
/**
13
 * 2.1.33 PropertyValue.
14
 *
15
 * A PropertyValue element is used to assign the result of an expression to a property of a term.
16
 *
17
 * The following is an example of the PropertyValue element.
18
 *
19
 *     <TypeAnnotation Term="ContactInfo">
20
 *         <PropertyValue Property="ContactName" String="ContactName1" />
21
 *     </TypeAnnotation>
22
 *
23
 * The following rules apply to the PropertyValue element:
24
 * - A PropertyValue MUST have a Property attribute defined that is of type SimpleIdentifier. Property names the
25
 *   property for which the value is supplied.
26
 * - A PropertyValue can specify an expression as a child element or as an expression attribute that gives the value of
27
 *   the property.
28
 * - A PropertyValue can have one of the following expression attributes defined in place of a child element expression.
29
 *   Each of these is equivalent to the same-named expression with the equivalent spelling:
30
 * - - Path
31
 * - - String
32
 * - - Int
33
 * - - Float
34
 * - - Decimal
35
 * - - Bool
36
 * - - DateTime
37
 *
38
 * The edm:PropertyValue element supplies a value to a property on the type instantiated by a type annotation. The
39
 * value is obtained by evaluating an expression.
40
 *
41
 *     <PropertyValue Property="ContactName" String="ContactName1" />
42
 *
43
 * @see https://www.odata.org/documentation/odata-version-3-0/common-schema-definition-language-csdl/#csdl15.3
44
 * XSD Type: TPropertyValue
45
 */
46
class PropertyValue extends EdmBase
47
{
48
    /**
49
     * @var string $property The property value element MUST assign a [simpleidentifier][csdl19] value to the
50
     *             edm:Property attribute. The value of the property attribute SHOULD resolve to a property on the term
51
     *             referenced by the type annotation.
52
     */
53
    private $property;
54
    /**
55
     * @var Expressions\ExpressionBase A property value MUST contain exactly one expression. The expression MAY be
56
     *                                 provided using element notation or attribute notation.
57
     */
58
    private $expression;
59
60
    /**
61
     * Gets as property.
62
     *
63
     * @return string
64
     */
65
    public function getProperty(): string
66
    {
67
        return $this->property;
68
    }
69
70
    /**
71
     * Sets a new property.
72
     *
73
     * @param  string $property
74
     * @return self
75
     */
76
    public function setProperty(string $property): self
77
    {
78
        $this->property = $property;
79
        return $this;
80
    }
81
82
    /**
83
     * Gets as expression.
84
     *
85
     * @return Expressions\ExpressionBase
86
     */
87
    public function getExpression()
88
    {
89
        return $this->expression;
90
    }
91
92
    /**
93
     * Sets a new valueTermReference.
94
     *
95
     * @param  Expressions\ExpressionBase $expression
96
     * @return self
97
     */
98
    public function setExpression(Expressions\ExpressionBase $expression): self
99
    {
100
        $this->expression = $expression;
101
        return $this;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getDomName(): string
108
    {
109
        return 'PropertyValue';
110
    }
111
112
    /**
113
     * @return array|AttributeContainer[]
114
     */
115
    public function getAttributes(): array
116
    {
117
        return $this->isAnnotation() ? [$this->expression] : [];
118
    }
119
120
    private function isAnnotation(){
121
        return (//  $this->expression instanceof Expressions\Dynamic\TPathExpression ||
122
            $this->expression instanceof Expressions\Constant\StringConstant ||
123
            $this->expression instanceof Expressions\Constant\IntConstant ||
124
            $this->expression instanceof Expressions\Constant\FloatConstant ||
125
            $this->expression instanceof Expressions\Constant\DecimalConstant ||
126
            $this->expression instanceof Expressions\Constant\BoolConstant ||
127
            $this->expression instanceof Expressions\Constant\DateTimeConstant
128
        );
129
    }
130
131
    /**
132
     * @return array|DomBase[]
133
     */
134
    public function getChildElements(): array
135
    {
136
        return $this->isAnnotation() ? [] : [$this->expression];
137
    }
138
}
139