commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
DateFieldModel.php
1<?php
2
3declare(strict_types=1);
10
15use DateTimeImmutable;
16use stdClass;
17
21final class DateFieldModel extends JsonObjectModel implements DateField
22{
23 public const DISCRIMINATOR_VALUE = 'Date';
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
74 public function getValue()
75 {
76 if (is_null($this->value)) {
78 $data = $this->raw(self::FIELD_VALUE);
79 if (is_null($data)) {
80 return null;
81 }
82 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATE_FORMAT, $data);
83 if (false === $data) {
84 return null;
85 }
86 $this->value = $data;
87 }
88
89 return $this->value;
90 }
91
92
96 public function setValue(?DateTimeImmutable $value): void
97 {
98 $this->value = $value;
99 }
100
101
102 #[\ReturnTypeWillChange]
103 public function jsonSerialize()
104 {
105 $data = $this->toArray();
106 if (isset($data[DateField::FIELD_VALUE]) && $data[DateField::FIELD_VALUE] instanceof \DateTimeImmutable) {
107 $data[DateField::FIELD_VALUE] = $data[DateField::FIELD_VALUE]->format('Y-m-d');
108 }
109 return (object) $data;
110 }
111}
__construct(?DateTimeImmutable $value=null, ?string $type=null)