ToTraceString::appendFacets()   D
last analyzed

Complexity

Conditions 23
Paths 23

Size

Total Lines 37
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 34
nc 23
nop 2
dl 0
loc 37
rs 4.1666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\ODataMetadata\Helpers;
6
7
use AlgoWeb\ODataMetadata\EdmConstants;
8
use AlgoWeb\ODataMetadata\Enums\PrimitiveTypeKind;
9
use AlgoWeb\ODataMetadata\Enums\TypeKind;
10
use AlgoWeb\ODataMetadata\Interfaces\IBinaryTypeReference;
11
use AlgoWeb\ODataMetadata\Interfaces\ICollectionType;
12
use AlgoWeb\ODataMetadata\Interfaces\IDecimalTypeReference;
13
use AlgoWeb\ODataMetadata\Interfaces\IEdmElement;
14
use AlgoWeb\ODataMetadata\Interfaces\IEntityReferenceType;
15
use AlgoWeb\ODataMetadata\Interfaces\IPrimitiveTypeReference;
16
use AlgoWeb\ODataMetadata\Interfaces\IProperty;
17
use AlgoWeb\ODataMetadata\Interfaces\IRowType;
18
use AlgoWeb\ODataMetadata\Interfaces\ISchemaElement;
19
use AlgoWeb\ODataMetadata\Interfaces\ISpatialTypeReference;
20
use AlgoWeb\ODataMetadata\Interfaces\IStringTypeReference;
21
use AlgoWeb\ODataMetadata\Interfaces\ITemporalTypeReference;
22
use AlgoWeb\ODataMetadata\Interfaces\ITypeReference;
23
24
abstract class ToTraceString
25
{
26
    /**
27
     * Returns the text representation of the current object.
28
     *
29
     * @param  IEdmElement|null $element reference to the calling object
30
     * @return string           the text representation of the current object
31
     */
32
    public static function toTraceString(?IEdmElement $element): string
33
    {
34
        switch (true) {
35
            case $element instanceof ISchemaElement:
36
                assert($element instanceof ISchemaElement);
37
                return $element->fullName();
38
            case $element instanceof ITypeReference:
39
                assert($element instanceof ITypeReference);
40
                $s = '[';
41
                $s .= self::toTraceString($element->getDefinition());
42
                $s = self::appendKeyValue(
43
                    $s,
44
                    EdmConstants::FacetName_Nullable,
45
                    $element->getNullable() ? 'TRUE' : 'FALSE'
46
                );
47
                if ($element->isPrimitive()) {
48
                    $s = self::appendFacets($s, $element->asPrimitive());
49
                }
50
                $s .= ']';
51
                return $s;
52
            case $element instanceof IProperty:
53
                assert($element instanceof IProperty);
54
                $type = $element->getType();
55
                return $element->getName() ?? '' . ':' . ($type !== null ? self::toTraceString($type) : '');
56
            case $element instanceof IEntityReferenceType:
57
                assert($element instanceof IEntityReferenceType);
58
                return strval(TypeKind::EntityReference()->getKey()) . '(' . (null !== $element->getEntityType() ?
59
                        self::toTraceString($element->getEntityType()) : '') . ')';
60
            case $element instanceof ICollectionType:
61
                assert($element instanceof ICollectionType);
62
                return strval(TypeKind::Collection()->getKey()) . '(' . (null !== $element->getElementType() ?
63
                        self::toTraceString($element->getElementType()) : '') . ')';
64
            case $element instanceof IRowType:
65
                assert($element instanceof IRowType);
66
                $s = TypeKind::Row()->getKey();
67
                $s .= '(';
68
                foreach ($element->properties() as $prop) {
69
                    if (null === $prop) {
70
                        continue;
71
                    }
72
                    $s .= self::toTraceString($prop);
73
                    $s .= ', ';
74
                }
75
                $s = substr($s, 0, -2);
76
                $s .= ')';
77
                return $s;
78
            default:
79
                return EdmConstants::Value_UnknownType;
80
        }
81
    }
82
83
    private static function appendFacets(string $s, IPrimitiveTypeReference $type): string
84
    {
85
        switch ($type->primitiveKind()) {
86
            case PrimitiveTypeKind::Binary():
87
                $s = self::appendBinaryFacets($s, $type->asBinary());
88
                break;
89
            case PrimitiveTypeKind::Decimal():
90
                $s = self::appendDecimalFacets($s, $type->asDecimal());
91
                break;
92
            case PrimitiveTypeKind::String():
93
                $s = self::appendStringFacets($s, $type->asString());
94
                break;
95
            case PrimitiveTypeKind::Time():
96
            case PrimitiveTypeKind::DateTime():
97
            case PrimitiveTypeKind::DateTimeOffset():
98
                $s = self::appendTemporalFacets($s, $type->asTemporal());
99
                break;
100
            case PrimitiveTypeKind::Geography():
101
            case PrimitiveTypeKind::GeographyPoint():
102
            case PrimitiveTypeKind::GeographyLineString():
103
            case PrimitiveTypeKind::GeographyPolygon():
104
            case PrimitiveTypeKind::GeographyCollection():
105
            case PrimitiveTypeKind::GeographyMultiPoint():
106
            case PrimitiveTypeKind::GeographyMultiLineString():
107
            case PrimitiveTypeKind::GeographyMultiPolygon():
108
            case PrimitiveTypeKind::Geometry():
109
            case PrimitiveTypeKind::GeometryPoint():
110
            case PrimitiveTypeKind::GeometryLineString():
111
            case PrimitiveTypeKind::GeometryPolygon():
112
            case PrimitiveTypeKind::GeometryCollection():
113
            case PrimitiveTypeKind::GeometryMultiPolygon():
114
            case PrimitiveTypeKind::GeometryMultiLineString():
115
            case PrimitiveTypeKind::GeometryMultiPoint():
116
                $s = self::appendSpatialFacets($s, $type->asSpatial());
117
                break;
118
        }
119
        return $s;
120
    }
121
122
123
    private static function appendBinaryFacets(string $sb, IBinaryTypeReference $type): string
124
    {
125
        self::appendKeyValue($sb, EdmConstants::FacetName_FixedLength, $type->isFixedLength() ? 'TRUE' : 'FALSE');
126
        if ($type->isUnBounded() || null !== $type->getMaxLength()) {
127
            $sb = self::appendKeyValue(
128
                $sb,
129
                EdmConstants::FacetName_MaxLength,
130
                ($type->isUnBounded()) ? EdmConstants::Value_Max : strval($type->getMaxLength())
131
            );
132
        }
133
        return $sb;
134
    }
135
136
    private static function appendStringFacets(string $sb, IStringTypeReference $type): string
137
    {
138
        if (true === $type->isFixedLength()) {
139
            $sb = self::appendKeyValue(
140
                $sb,
141
                EdmConstants::FacetName_FixedLength,
142
                'TRUE'
143
            );
144
        }
145
146
        if (true === $type->isUnbounded() || null !== $type->getMaxLength()) {
147
            $sb = self::appendKeyValue(
148
                $sb,
149
                EdmConstants::FacetName_MaxLength,
150
                $type->isUnbounded() ? EdmConstants::Value_Max : $type->getMaxLength()
151
            );
152
        }
153
154
        if (true === $type->isUnicode()) {
155
            $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Unicode, 'TRUE');
156
        }
157
158
        if (null !== $type->getCollation()) {
159
            $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Collation, $type->getCollation());
160
        }
161
        return $sb;
162
    }
163
164
    private static function appendTemporalFacets(string $sb, ITemporalTypeReference $type): string
165
    {
166
        if (null !== $type->getPrecision()) {
167
            $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Precision, $type->getPrecision());
168
        }
169
        return $sb;
170
    }
171
172
    private static function appendDecimalFacets(string $sb, IDecimalTypeReference $type): string
173
    {
174
        if (null !== $type->getPrecision()) {
175
            $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Precision, $type->getPrecision());
176
        }
177
178
        if (null !== $type->getScale()) {
179
            $sb = self::appendKeyValue($sb, EdmConstants::FacetName_Scale, $type->getScale());
180
        }
181
        return $sb;
182
    }
183
184
    private static function appendSpatialFacets(string $sb, ISpatialTypeReference $type): string
185
    {
186
        $sb = self::appendKeyValue(
187
            $sb,
188
            EdmConstants::FacetName_Srid,
189
            null !== $type->getSpatialReferenceIdentifier()
190
                ? $type->getSpatialReferenceIdentifier() : EdmConstants::Value_SridVariable
191
        );
192
        return $sb;
193
    }
194
195
    /**
196
     * @param  string          $s
197
     * @param  string          $key
198
     * @param  string|int|null $value
199
     * @return string
200
     */
201
    private static function appendKeyValue(string $s, string $key, $value)
202
    {
203
        $s .= ' ';
204
        $s .= $key;
205
        $s .= '=';
206
        $s .= $value;
207
        return $s;
208
    }
209
}
210