Completed
Push — master ( e5c932...e18320 )
by Alex
02:04
created

EntityField::setIsKeyField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities;
4
5
use POData\Providers\Metadata\Type\EdmPrimitiveType;
6
use POData\Providers\Metadata\Type\TypeCode;
7
8
class EntityField
9
{
10
    /**
11
     * @var string
12
     */
13
    private $name;
14
15
    /**
16
     * @var EntityFieldType;
17
     */
18
    private $fieldType;
19
20
    /**
21
     * @var bool
22
     */
23
    private $isNullable;
24
25
    /**
26
     * @var mixed
27
     */
28
    private $defaultValue;
29
30
    /**
31
     * @var bool
32
     */
33
    private $readOnly;
34
35
    /**
36
     * @var bool
37
     */
38
    private $createOnly;
39
40
    /**
41
     * @var bool
42
     */
43
    private $isKeyField;
44
45
    /**
46
     * @var EntityFieldPrimitiveType
47
     */
48
    private $primitiveType;
49
    /**
50
     * @var TypeCode
51
     */
52
    private $edmFieldType;
53
54
    /**
55
     * @return \POData\Providers\Metadata\Type\TypeCode
56
     */
57
    public function getEdmFieldType()
58
    {
59
        return $this->edmFieldType;
60
    }
61
62
    /**
63
     * @return \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType
64
     */
65
    public function getPrimitiveType()
66
    {
67
        return $this->primitiveType;
68
    }
69
70
    /**
71
     * @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType $primitiveType
72
     */
73
    public function setPrimitiveType(EntityFieldPrimitiveType $primitiveType)
74
    {
75
        $this->primitiveType = $primitiveType;
76
        $this->edmFieldType = $this->primitiveTypeToEdmType($primitiveType);
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getName()
83
    {
84
        return $this->name;
85
    }
86
87
    /**
88
     * @param string $name
89
     */
90
    public function setName($name)
91
    {
92
        $this->name = $name;
93
    }
94
95
    /**
96
     * @return EntityFieldType
97
     */
98
    public function getFieldType()
99
    {
100
        return $this->fieldType;
101
    }
102
103
    /**
104
     * @param EntityFieldType $fieldType
105
     */
106
    public function setFieldType(EntityFieldType $fieldType)
107
    {
108
        $this->fieldType = $fieldType;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function getIsNullable()
115
    {
116
        return $this->isNullable;
117
    }
118
119
    /**
120
     * @param bool $isNullable
121
     */
122
    public function setIsNullable($isNullable)
123
    {
124
        $this->isNullable = boolval($isNullable);
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130
    public function getDefaultValue()
131
    {
132
        return $this->defaultValue;
133
    }
134
135
    /**
136
     * @param mixed $defaultValue
137
     */
138
    public function setDefaultValue($defaultValue)
139
    {
140
        $this->defaultValue = $defaultValue;
141
    }
142
143
    /**
144
     * @return bool
145
     */
146
    public function getReadOnly()
147
    {
148
        return $this->readOnly;
149
    }
150
151
    /**
152
     * @param bool $readOnly
153
     */
154
    public function setReadOnly($readOnly)
155
    {
156
        $this->readOnly = boolval($readOnly);
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function getCreateOnly()
163
    {
164
        return $this->createOnly;
165
    }
166
167
    /**
168
     * @param bool $createOnly
169
     */
170
    public function setCreateOnly($createOnly)
171
    {
172
        $this->createOnly = boolval($createOnly);
173
    }
174
175
    /**
176
     * @return bool
177
     */
178
    public function getIsKeyField()
179
    {
180
        return $this->isKeyField;
181
    }
182
183
    /**
184
     * @param bool $keyField
185
     */
186
    public function setIsKeyField($keyField)
187
    {
188
        $this->isKeyField = boolval($keyField);
189
    }
190
191
    /**
192
     * @var array
193
     */
194
    private static $primitiveToEdmMapping = [
195
        EntityFieldPrimitiveType::INTEGER => EdmPrimitiveType::INT32,
196
        EntityFieldPrimitiveType::STRING => EdmPrimitiveType::STRING,
197
        EntityFieldPrimitiveType::DATETIME => EdmPrimitiveType::DATETIME,
198
        EntityFieldPrimitiveType::FLOAT => EdmPrimitiveType::SINGLE,
199
        EntityFieldPrimitiveType::DECIMAL => EdmPrimitiveType::DECIMAL,
200
        EntityFieldPrimitiveType::STRING => EdmPrimitiveType::STRING,
201
        EntityFieldPrimitiveType::BOOLEAN => EdmPrimitiveType::BOOLEAN,
202
        EntityFieldPrimitiveType::BLOB => 'stream'
203
    ];
204
205
    /**
206
     * @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType $primitiveType
207
     *
208
     * @return TypeCode
209
     */
210
    private function primitiveTypeToEdmType(EntityFieldPrimitiveType $primitiveType)
211
    {
212
        $value = $primitiveType->getValue();
213
        return array_key_exists($value, self::$primitiveToEdmMapping) ?
214
            self::$primitiveToEdmMapping[$value] : EdmPrimitiveType::STRING;
215
    }
216
}
217