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
621use DateTimeImmutable;
622use stdClass;
623
627final class BaseResourceModel extends JsonObjectModel implements BaseResource
628{
633 protected $id;
634
639 protected $version;
640
645 protected $createdAt;
646
652
653
657 public function __construct(
658 ?string $id = null,
659 ?int $version = null,
660 ?DateTimeImmutable $createdAt = null,
661 ?DateTimeImmutable $lastModifiedAt = null
662 ) {
663 $this->id = $id;
664 $this->version = $version;
665 $this->createdAt = $createdAt;
666 $this->lastModifiedAt = $lastModifiedAt;
667 }
668
673 public function getId()
674 {
675 if (is_null($this->id)) {
677 $data = $this->raw(self::FIELD_ID);
678 if (is_null($data)) {
679 return null;
680 }
681 $this->id = (string) $data;
682 }
683
684 return $this->id;
685 }
686
691 public function getVersion()
692 {
693 if (is_null($this->version)) {
695 $data = $this->raw(self::FIELD_VERSION);
696 if (is_null($data)) {
697 return null;
698 }
699 $this->version = (int) $data;
700 }
701
702 return $this->version;
703 }
704
709 public function getCreatedAt()
710 {
711 if (is_null($this->createdAt)) {
713 $data = $this->raw(self::FIELD_CREATED_AT);
714 if (is_null($data)) {
715 return null;
716 }
717 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
718 if (false === $data) {
719 return null;
720 }
721 $this->createdAt = $data;
722 }
723
724 return $this->createdAt;
725 }
726
731 public function getLastModifiedAt()
732 {
733 if (is_null($this->lastModifiedAt)) {
735 $data = $this->raw(self::FIELD_LAST_MODIFIED_AT);
736 if (is_null($data)) {
737 return null;
738 }
739 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
740 if (false === $data) {
741 return null;
742 }
743 $this->lastModifiedAt = $data;
744 }
745
747 }
748
749
753 public function setId(?string $id): void
754 {
755 $this->id = $id;
756 }
757
761 public function setVersion(?int $version): void
762 {
763 $this->version = $version;
764 }
765
769 public function setCreatedAt(?DateTimeImmutable $createdAt): void
770 {
771 $this->createdAt = $createdAt;
772 }
773
777 public function setLastModifiedAt(?DateTimeImmutable $lastModifiedAt): void
778 {
779 $this->lastModifiedAt = $lastModifiedAt;
780 }
781
782
783 #[\ReturnTypeWillChange]
784 public function jsonSerialize()
785 {
786 $data = $this->toArray();
787 if (isset($data[BaseResource::FIELD_CREATED_AT]) && $data[BaseResource::FIELD_CREATED_AT] instanceof \DateTimeImmutable) {
788 $data[BaseResource::FIELD_CREATED_AT] = $data[BaseResource::FIELD_CREATED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
789 }
790
791 if (isset($data[BaseResource::FIELD_LAST_MODIFIED_AT]) && $data[BaseResource::FIELD_LAST_MODIFIED_AT] instanceof \DateTimeImmutable) {
792 $data[BaseResource::FIELD_LAST_MODIFIED_AT] = $data[BaseResource::FIELD_LAST_MODIFIED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
793 }
794 return (object) $data;
795 }
796}
__construct(?string $id=null, ?int $version=null, ?DateTimeImmutable $createdAt=null, ?DateTimeImmutable $lastModifiedAt=null)
setLastModifiedAt(?DateTimeImmutable $lastModifiedAt)