|
5 | 5 |
|
6 | 6 | namespace Commercetools\Core\Model\CustomField; |
7 | 7 |
|
| 8 | +use Commercetools\Core\Model\Common\DateDecorator; |
| 9 | +use Commercetools\Core\Model\Common\DateTimeDecorator; |
| 10 | +use Commercetools\Core\Model\Common\Enum; |
8 | 11 | use Commercetools\Core\Model\Common\JsonObject; |
| 12 | +use Commercetools\Core\Model\Common\LocalizedEnum; |
| 13 | +use Commercetools\Core\Model\Common\LocalizedString; |
| 14 | +use Commercetools\Core\Model\Common\Money; |
| 15 | +use Commercetools\Core\Model\Common\Reference; |
| 16 | +use Commercetools\Core\Model\Common\Set; |
| 17 | +use Commercetools\Core\Model\Common\TimeDecorator; |
9 | 18 | use Commercetools\Core\Model\Type\FieldDefinition; |
10 | 19 | use Commercetools\Core\Model\Type\FieldDefinitionCollection; |
11 | 20 | use Commercetools\Core\Model\Type\FieldType; |
@@ -61,4 +70,298 @@ protected function isValidField($field) |
61 | 70 | { |
62 | 71 | return true; |
63 | 72 | } |
| 73 | + |
| 74 | + /** |
| 75 | + * @return bool |
| 76 | + */ |
| 77 | + public function getFieldAsBool($name) |
| 78 | + { |
| 79 | + return (bool)$this->get($name); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return float |
| 84 | + */ |
| 85 | + public function getFieldAsNumber($name) |
| 86 | + { |
| 87 | + $value = $this->get($name); |
| 88 | + return (float)$value; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @return int |
| 93 | + */ |
| 94 | + public function getFieldAsInteger($name) |
| 95 | + { |
| 96 | + $value = $this->get($name); |
| 97 | + return (int)$value; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + public function getFieldAsString($name) |
| 104 | + { |
| 105 | + return (string)$this->get($name); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return LocalizedString |
| 110 | + */ |
| 111 | + public function getFieldAsLocalizedString($name) |
| 112 | + { |
| 113 | + $value = $this->get($name); |
| 114 | + |
| 115 | + if ($value instanceof LocalizedString) { |
| 116 | + return $value; |
| 117 | + } |
| 118 | + return LocalizedString::fromArray($value); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return LocalizedEnum |
| 123 | + */ |
| 124 | + public function getFieldAsLocalizedEnum($name) |
| 125 | + { |
| 126 | + $value = $this->get($name); |
| 127 | + |
| 128 | + if ($value instanceof LocalizedEnum) { |
| 129 | + return $value; |
| 130 | + } |
| 131 | + return LocalizedEnum::fromArray($value); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @return Enum |
| 136 | + */ |
| 137 | + public function getFieldAsEnum($name) |
| 138 | + { |
| 139 | + $value = $this->get($name); |
| 140 | + |
| 141 | + if ($value instanceof Enum) { |
| 142 | + return $value; |
| 143 | + } |
| 144 | + return Enum::fromArray($value); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @return Money |
| 149 | + */ |
| 150 | + public function getFieldAsMoney($name) |
| 151 | + { |
| 152 | + $value = $this->get($name); |
| 153 | + |
| 154 | + if ($value instanceof Money) { |
| 155 | + return $value; |
| 156 | + } |
| 157 | + return Money::fromArray($value); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return DateDecorator |
| 162 | + */ |
| 163 | + public function getFieldAsDate($name) |
| 164 | + { |
| 165 | + $value = $this->get($name); |
| 166 | + |
| 167 | + if ($value instanceof DateDecorator) { |
| 168 | + return $value; |
| 169 | + } |
| 170 | + return new DateDecorator($value); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @return TimeDecorator |
| 175 | + */ |
| 176 | + public function getFieldAsTime($name) |
| 177 | + { |
| 178 | + $value = $this->get($name); |
| 179 | + |
| 180 | + if ($value instanceof TimeDecorator) { |
| 181 | + return $value; |
| 182 | + } |
| 183 | + return new TimeDecorator($value); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * @return DateTimeDecorator |
| 188 | + */ |
| 189 | + public function getFieldAsDateTime($name) |
| 190 | + { |
| 191 | + $value = $this->get($name); |
| 192 | + |
| 193 | + if ($value instanceof DateTimeDecorator) { |
| 194 | + return $value; |
| 195 | + } |
| 196 | + return new DateTimeDecorator($value); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @return Reference |
| 201 | + */ |
| 202 | + public function getFieldAsReference($name) |
| 203 | + { |
| 204 | + $value = $this->get($name); |
| 205 | + |
| 206 | + if ($value instanceof Reference) { |
| 207 | + return $value; |
| 208 | + } |
| 209 | + return Reference::fromArray($value); |
| 210 | + } |
| 211 | + |
| 212 | + /** |
| 213 | + * @return Set |
| 214 | + */ |
| 215 | + public function getFieldAsBoolSet($name) |
| 216 | + { |
| 217 | + $value = $this->get($name); |
| 218 | + |
| 219 | + if ($value instanceof Set && $value->getType() == 'bool') { |
| 220 | + return $value; |
| 221 | + } |
| 222 | + return Set::ofTypeAndData('bool', $value); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * @return Set |
| 227 | + */ |
| 228 | + public function getFieldAsNumberSet($name) |
| 229 | + { |
| 230 | + $value = $this->get($name); |
| 231 | + |
| 232 | + if ($value instanceof Set && $value->getType() == 'float') { |
| 233 | + return $value; |
| 234 | + } |
| 235 | + return Set::ofTypeAndData('float', $value); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * @return Set |
| 240 | + */ |
| 241 | + public function getFieldAsIntegerSet($name) |
| 242 | + { |
| 243 | + $value = $this->get($name); |
| 244 | + |
| 245 | + if ($value instanceof Set && $value->getType() == 'int') { |
| 246 | + return $value; |
| 247 | + } |
| 248 | + return Set::ofTypeAndData('int', $value); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * @return Set |
| 253 | + */ |
| 254 | + public function getFieldAsStringSet($name) |
| 255 | + { |
| 256 | + $value = $this->get($name); |
| 257 | + |
| 258 | + if ($value instanceof Set && $value->getType() == 'string') { |
| 259 | + return $value; |
| 260 | + } |
| 261 | + return Set::ofTypeAndData('string', $value); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * @return Set |
| 266 | + */ |
| 267 | + public function getFieldAsLocalizedStringSet($name) |
| 268 | + { |
| 269 | + $value = $this->get($name); |
| 270 | + |
| 271 | + if ($value instanceof Set && $value->getType() == LocalizedString::class) { |
| 272 | + return $value; |
| 273 | + } |
| 274 | + return Set::ofTypeAndData(LocalizedString::class, $value); |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * @return Set |
| 279 | + */ |
| 280 | + public function getFieldAsLocalizedEnumSet($name) |
| 281 | + { |
| 282 | + $value = $this->get($name); |
| 283 | + |
| 284 | + if ($value instanceof Set && $value->getType() == LocalizedEnum::class) { |
| 285 | + return $value; |
| 286 | + } |
| 287 | + return Set::ofTypeAndData(LocalizedEnum::class, $value); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * @return Set |
| 292 | + */ |
| 293 | + public function getFieldAsEnumSet($name) |
| 294 | + { |
| 295 | + $value = $this->get($name); |
| 296 | + |
| 297 | + if ($value instanceof Set && $value->getType() == Enum::class) { |
| 298 | + return $value; |
| 299 | + } |
| 300 | + return Set::ofTypeAndData(Enum::class, $value); |
| 301 | + } |
| 302 | + |
| 303 | + /** |
| 304 | + * @return Set |
| 305 | + */ |
| 306 | + public function getFieldAsMoneySet($name) |
| 307 | + { |
| 308 | + $value = $this->get($name); |
| 309 | + |
| 310 | + if ($value instanceof Set && $value->getType() == Money::class) { |
| 311 | + return $value; |
| 312 | + } |
| 313 | + return Set::ofTypeAndData(Money::class, $value); |
| 314 | + } |
| 315 | + |
| 316 | + /** |
| 317 | + * @return Set |
| 318 | + */ |
| 319 | + public function getFieldAsDateSet($name) |
| 320 | + { |
| 321 | + $value = $this->get($name); |
| 322 | + |
| 323 | + if ($value instanceof Set && $value->getType() == DateDecorator::class) { |
| 324 | + return $value; |
| 325 | + } |
| 326 | + return Set::ofTypeAndData(DateDecorator::class, $value); |
| 327 | + } |
| 328 | + |
| 329 | + /** |
| 330 | + * @return Set |
| 331 | + */ |
| 332 | + public function getFieldAsTimeSet($name) |
| 333 | + { |
| 334 | + $value = $this->get($name); |
| 335 | + |
| 336 | + if ($value instanceof Set && $value->getType() == TimeDecorator::class) { |
| 337 | + return $value; |
| 338 | + } |
| 339 | + return Set::ofTypeAndData(TimeDecorator::class, $value); |
| 340 | + } |
| 341 | + |
| 342 | + /** |
| 343 | + * @return Set |
| 344 | + */ |
| 345 | + public function getFieldAsDateTimeSet($name) |
| 346 | + { |
| 347 | + $value = $this->get($name); |
| 348 | + |
| 349 | + if ($value instanceof Set && $value->getType() == DateTimeDecorator::class) { |
| 350 | + return $value; |
| 351 | + } |
| 352 | + return Set::ofTypeAndData(DateTimeDecorator::class, $value); |
| 353 | + } |
| 354 | + |
| 355 | + /** |
| 356 | + * @return Set |
| 357 | + */ |
| 358 | + public function getFieldAsReferenceSet($name) |
| 359 | + { |
| 360 | + $value = $this->get($name); |
| 361 | + |
| 362 | + if ($value instanceof Set && $value->getType() == Reference::class) { |
| 363 | + return $value; |
| 364 | + } |
| 365 | + return Set::ofTypeAndData(Reference::class, $value); |
| 366 | + } |
64 | 367 | } |
0 commit comments