commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
BaseResourceModel.php
1<?php
2
3declare(strict_types=1);
10
595use DateTimeImmutable;
596use stdClass;
597
601final class BaseResourceModel extends JsonObjectModel implements BaseResource
602{
607 protected $id;
608
613 protected $version;
614
619 protected $createdAt;
620
626
627
631 public function __construct(
632 ?string $id = null,
633 ?int $version = null,
634 ?DateTimeImmutable $createdAt = null,
635 ?DateTimeImmutable $lastModifiedAt = null
636 ) {
637 $this->id = $id;
638 $this->version = $version;
639 $this->createdAt = $createdAt;
640 $this->lastModifiedAt = $lastModifiedAt;
641 }
642
647 public function getId()
648 {
649 if (is_null($this->id)) {
651 $data = $this->raw(self::FIELD_ID);
652 if (is_null($data)) {
653 return null;
654 }
655 $this->id = (string) $data;
656 }
657
658 return $this->id;
659 }
660
665 public function getVersion()
666 {
667 if (is_null($this->version)) {
669 $data = $this->raw(self::FIELD_VERSION);
670 if (is_null($data)) {
671 return null;
672 }
673 $this->version = (int) $data;
674 }
675
676 return $this->version;
677 }
678
683 public function getCreatedAt()
684 {
685 if (is_null($this->createdAt)) {
687 $data = $this->raw(self::FIELD_CREATED_AT);
688 if (is_null($data)) {
689 return null;
690 }
691 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
692 if (false === $data) {
693 return null;
694 }
695 $this->createdAt = $data;
696 }
697
698 return $this->createdAt;
699 }
700
705 public function getLastModifiedAt()
706 {
707 if (is_null($this->lastModifiedAt)) {
709 $data = $this->raw(self::FIELD_LAST_MODIFIED_AT);
710 if (is_null($data)) {
711 return null;
712 }
713 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
714 if (false === $data) {
715 return null;
716 }
717 $this->lastModifiedAt = $data;
718 }
719
721 }
722
723
727 public function setId(?string $id): void
728 {
729 $this->id = $id;
730 }
731
735 public function setVersion(?int $version): void
736 {
737 $this->version = $version;
738 }
739
743 public function setCreatedAt(?DateTimeImmutable $createdAt): void
744 {
745 $this->createdAt = $createdAt;
746 }
747
751 public function setLastModifiedAt(?DateTimeImmutable $lastModifiedAt): void
752 {
753 $this->lastModifiedAt = $lastModifiedAt;
754 }
755
756
757 #[\ReturnTypeWillChange]
758 public function jsonSerialize()
759 {
760 $data = $this->toArray();
761 if (isset($data[BaseResource::FIELD_CREATED_AT]) && $data[BaseResource::FIELD_CREATED_AT] instanceof \DateTimeImmutable) {
762 $data[BaseResource::FIELD_CREATED_AT] = $data[BaseResource::FIELD_CREATED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
763 }
764
765 if (isset($data[BaseResource::FIELD_LAST_MODIFIED_AT]) && $data[BaseResource::FIELD_LAST_MODIFIED_AT] instanceof \DateTimeImmutable) {
766 $data[BaseResource::FIELD_LAST_MODIFIED_AT] = $data[BaseResource::FIELD_LAST_MODIFIED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
767 }
768 return (object) $data;
769 }
770}
__construct(?string $id=null, ?int $version=null, ?DateTimeImmutable $createdAt=null, ?DateTimeImmutable $lastModifiedAt=null)
setLastModifiedAt(?DateTimeImmutable $lastModifiedAt)