commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
All Classes Namespaces Functions Variables Pages
BaseResourceModel.php
1<?php
2
3declare(strict_types=1);
10
493use DateTimeImmutable;
494use stdClass;
495
499final class BaseResourceModel extends JsonObjectModel implements BaseResource
500{
505 protected $id;
506
511 protected $version;
512
517 protected $createdAt;
518
524
525
529 public function __construct(
530 ?string $id = null,
531 ?int $version = null,
532 ?DateTimeImmutable $createdAt = null,
533 ?DateTimeImmutable $lastModifiedAt = null
534 ) {
535 $this->id = $id;
536 $this->version = $version;
537 $this->createdAt = $createdAt;
538 $this->lastModifiedAt = $lastModifiedAt;
539 }
540
545 public function getId()
546 {
547 if (is_null($this->id)) {
549 $data = $this->raw(self::FIELD_ID);
550 if (is_null($data)) {
551 return null;
552 }
553 $this->id = (string) $data;
554 }
555
556 return $this->id;
557 }
558
563 public function getVersion()
564 {
565 if (is_null($this->version)) {
567 $data = $this->raw(self::FIELD_VERSION);
568 if (is_null($data)) {
569 return null;
570 }
571 $this->version = (int) $data;
572 }
573
574 return $this->version;
575 }
576
581 public function getCreatedAt()
582 {
583 if (is_null($this->createdAt)) {
585 $data = $this->raw(self::FIELD_CREATED_AT);
586 if (is_null($data)) {
587 return null;
588 }
589 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
590 if (false === $data) {
591 return null;
592 }
593 $this->createdAt = $data;
594 }
595
596 return $this->createdAt;
597 }
598
603 public function getLastModifiedAt()
604 {
605 if (is_null($this->lastModifiedAt)) {
607 $data = $this->raw(self::FIELD_LAST_MODIFIED_AT);
608 if (is_null($data)) {
609 return null;
610 }
611 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
612 if (false === $data) {
613 return null;
614 }
615 $this->lastModifiedAt = $data;
616 }
617
619 }
620
621
625 public function setId(?string $id): void
626 {
627 $this->id = $id;
628 }
629
633 public function setVersion(?int $version): void
634 {
635 $this->version = $version;
636 }
637
641 public function setCreatedAt(?DateTimeImmutable $createdAt): void
642 {
643 $this->createdAt = $createdAt;
644 }
645
649 public function setLastModifiedAt(?DateTimeImmutable $lastModifiedAt): void
650 {
651 $this->lastModifiedAt = $lastModifiedAt;
652 }
653
654
655 #[\ReturnTypeWillChange]
656 public function jsonSerialize()
657 {
658 $data = $this->toArray();
659 if (isset($data[BaseResource::FIELD_CREATED_AT]) && $data[BaseResource::FIELD_CREATED_AT] instanceof \DateTimeImmutable) {
660 $data[BaseResource::FIELD_CREATED_AT] = $data[BaseResource::FIELD_CREATED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
661 }
662
663 if (isset($data[BaseResource::FIELD_LAST_MODIFIED_AT]) && $data[BaseResource::FIELD_LAST_MODIFIED_AT] instanceof \DateTimeImmutable) {
664 $data[BaseResource::FIELD_LAST_MODIFIED_AT] = $data[BaseResource::FIELD_LAST_MODIFIED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
665 }
666 return (object) $data;
667 }
668}
__construct(?string $id=null, ?int $version=null, ?DateTimeImmutable $createdAt=null, ?DateTimeImmutable $lastModifiedAt=null)
setLastModifiedAt(?DateTimeImmutable $lastModifiedAt)