commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
TimeAttributeModel.php
1<?php
2
3declare(strict_types=1);
10
15use DateTimeImmutable;
16use stdClass;
17
21final class TimeAttributeModel extends JsonObjectModel implements TimeAttribute
22{
23 public const DISCRIMINATOR_VALUE = 'time';
28 protected $name;
29
34 protected $type;
35
40 protected $value;
41
42
46 public function __construct(
47 ?string $name = null,
48 ?DateTimeImmutable $value = null,
49 ?string $type = null
50 ) {
51 $this->name = $name;
52 $this->value = $value;
53 $this->type = $type ?? self::DISCRIMINATOR_VALUE;
54 }
55
64 public function getName()
65 {
66 if (is_null($this->name)) {
68 $data = $this->raw(self::FIELD_NAME);
69 if (is_null($data)) {
70 return null;
71 }
72 $this->name = (string) $data;
73 }
74
75 return $this->name;
76 }
77
86 public function getType()
87 {
88 if (is_null($this->type)) {
90 $data = $this->raw(self::FIELD_TYPE);
91 if (is_null($data)) {
92 return null;
93 }
94 $this->type = (string) $data;
95 }
96
97 return $this->type;
98 }
99
108 public function getValue()
109 {
110 if (is_null($this->value)) {
112 $data = $this->raw(self::FIELD_VALUE);
113 if (is_null($data)) {
114 return null;
115 }
116 $data = DateTimeImmutable::createFromFormat(MapperFactory::TIME_FORMAT, $data);
117 if (false === $data) {
118 return null;
119 }
120 $this->value = $data;
121 }
122
123 return $this->value;
124 }
125
126
130 public function setName(?string $name): void
131 {
132 $this->name = $name;
133 }
134
138 public function setValue(?DateTimeImmutable $value): void
139 {
140 $this->value = $value;
141 }
142
143
144 #[\ReturnTypeWillChange]
145 public function jsonSerialize()
146 {
147 $data = $this->toArray();
148 if (isset($data[TimeAttribute::FIELD_VALUE]) && $data[TimeAttribute::FIELD_VALUE] instanceof \DateTimeImmutable) {
149 $data[TimeAttribute::FIELD_VALUE] = $data[TimeAttribute::FIELD_VALUE]->format('H:i:s.u');
150 }
151 return (object) $data;
152 }
153}
__construct(?string $name=null, ?DateTimeImmutable $value=null, ?string $type=null)