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
681use DateTimeImmutable;
682use stdClass;
683
687final class BaseResourceModel extends JsonObjectModel implements BaseResource
688{
693 protected $id;
694
699 protected $version;
700
705 protected $createdAt;
706
712
713
717 public function __construct(
718 ?string $id = null,
719 ?int $version = null,
720 ?DateTimeImmutable $createdAt = null,
721 ?DateTimeImmutable $lastModifiedAt = null
722 ) {
723 $this->id = $id;
724 $this->version = $version;
725 $this->createdAt = $createdAt;
726 $this->lastModifiedAt = $lastModifiedAt;
727 }
728
733 public function getId()
734 {
735 if (is_null($this->id)) {
737 $data = $this->raw(self::FIELD_ID);
738 if (is_null($data)) {
739 return null;
740 }
741 $this->id = (string) $data;
742 }
743
744 return $this->id;
745 }
746
751 public function getVersion()
752 {
753 if (is_null($this->version)) {
755 $data = $this->raw(self::FIELD_VERSION);
756 if (is_null($data)) {
757 return null;
758 }
759 $this->version = (int) $data;
760 }
761
762 return $this->version;
763 }
764
769 public function getCreatedAt()
770 {
771 if (is_null($this->createdAt)) {
773 $data = $this->raw(self::FIELD_CREATED_AT);
774 if (is_null($data)) {
775 return null;
776 }
777 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
778 if (false === $data) {
779 return null;
780 }
781 $this->createdAt = $data;
782 }
783
784 return $this->createdAt;
785 }
786
791 public function getLastModifiedAt()
792 {
793 if (is_null($this->lastModifiedAt)) {
795 $data = $this->raw(self::FIELD_LAST_MODIFIED_AT);
796 if (is_null($data)) {
797 return null;
798 }
799 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
800 if (false === $data) {
801 return null;
802 }
803 $this->lastModifiedAt = $data;
804 }
805
807 }
808
809
813 public function setId(?string $id): void
814 {
815 $this->id = $id;
816 }
817
821 public function setVersion(?int $version): void
822 {
823 $this->version = $version;
824 }
825
829 public function setCreatedAt(?DateTimeImmutable $createdAt): void
830 {
831 $this->createdAt = $createdAt;
832 }
833
837 public function setLastModifiedAt(?DateTimeImmutable $lastModifiedAt): void
838 {
839 $this->lastModifiedAt = $lastModifiedAt;
840 }
841
842
843 #[\ReturnTypeWillChange]
844 public function jsonSerialize()
845 {
846 $data = $this->toArray();
847 if (isset($data[BaseResource::FIELD_CREATED_AT]) && $data[BaseResource::FIELD_CREATED_AT] instanceof \DateTimeImmutable) {
848 $data[BaseResource::FIELD_CREATED_AT] = $data[BaseResource::FIELD_CREATED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
849 }
850
851 if (isset($data[BaseResource::FIELD_LAST_MODIFIED_AT]) && $data[BaseResource::FIELD_LAST_MODIFIED_AT] instanceof \DateTimeImmutable) {
852 $data[BaseResource::FIELD_LAST_MODIFIED_AT] = $data[BaseResource::FIELD_LAST_MODIFIED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
853 }
854 return (object) $data;
855 }
856}
__construct(?string $id=null, ?int $version=null, ?DateTimeImmutable $createdAt=null, ?DateTimeImmutable $lastModifiedAt=null)
setLastModifiedAt(?DateTimeImmutable $lastModifiedAt)