commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
TimeFieldModel.php
1<?php
2
3declare(strict_types=1);
10
15use DateTimeImmutable;
16use stdClass;
17
21final class TimeFieldModel extends JsonObjectModel implements TimeField
22{
23 public const DISCRIMINATOR_VALUE = 'Time';
28 protected $type;
29
34 protected $value;
35
36
40 public function __construct(
41 ?DateTimeImmutable $value = null,
42 ?string $type = null
43 ) {
44 $this->value = $value;
45 $this->type = $type ?? self::DISCRIMINATOR_VALUE;
46 }
47
54 public function getType()
55 {
56 if (is_null($this->type)) {
58 $data = $this->raw(self::FIELD_TYPE);
59 if (is_null($data)) {
60 return null;
61 }
62 $this->type = (string) $data;
63 }
64
65 return $this->type;
66 }
67
76 public function getValue()
77 {
78 if (is_null($this->value)) {
80 $data = $this->raw(self::FIELD_VALUE);
81 if (is_null($data)) {
82 return null;
83 }
84 $data = DateTimeImmutable::createFromFormat(MapperFactory::TIME_FORMAT, $data);
85 if (false === $data) {
86 return null;
87 }
88 $this->value = $data;
89 }
90
91 return $this->value;
92 }
93
94
98 public function setValue(?DateTimeImmutable $value): void
99 {
100 $this->value = $value;
101 }
102
103
104 #[\ReturnTypeWillChange]
105 public function jsonSerialize()
106 {
107 $data = $this->toArray();
108 if (isset($data[TimeField::FIELD_VALUE]) && $data[TimeField::FIELD_VALUE] instanceof \DateTimeImmutable) {
109 $data[TimeField::FIELD_VALUE] = $data[TimeField::FIELD_VALUE]->format('H:i:s.u');
110 }
111 return (object) $data;
112 }
113}
__construct(?DateTimeImmutable $value=null, ?string $type=null)