commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
BaseResourceModel.php
1 <?php
2 
3 declare(strict_types=1);
10 
481 use DateTimeImmutable;
482 use stdClass;
483 
487 final class BaseResourceModel extends JsonObjectModel implements BaseResource
488 {
493  protected $id;
494 
499  protected $version;
500 
505  protected $createdAt;
506 
511  protected $lastModifiedAt;
512 
513 
517  public function __construct(
518  ?string $id = null,
519  ?int $version = null,
520  ?DateTimeImmutable $createdAt = null,
521  ?DateTimeImmutable $lastModifiedAt = null
522  ) {
523  $this->id = $id;
524  $this->version = $version;
525  $this->createdAt = $createdAt;
526  $this->lastModifiedAt = $lastModifiedAt;
527  }
528 
533  public function getId()
534  {
535  if (is_null($this->id)) {
537  $data = $this->raw(self::FIELD_ID);
538  if (is_null($data)) {
539  return null;
540  }
541  $this->id = (string) $data;
542  }
543 
544  return $this->id;
545  }
546 
551  public function getVersion()
552  {
553  if (is_null($this->version)) {
555  $data = $this->raw(self::FIELD_VERSION);
556  if (is_null($data)) {
557  return null;
558  }
559  $this->version = (int) $data;
560  }
561 
562  return $this->version;
563  }
564 
569  public function getCreatedAt()
570  {
571  if (is_null($this->createdAt)) {
573  $data = $this->raw(self::FIELD_CREATED_AT);
574  if (is_null($data)) {
575  return null;
576  }
577  $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
578  if (false === $data) {
579  return null;
580  }
581  $this->createdAt = $data;
582  }
583 
584  return $this->createdAt;
585  }
586 
591  public function getLastModifiedAt()
592  {
593  if (is_null($this->lastModifiedAt)) {
595  $data = $this->raw(self::FIELD_LAST_MODIFIED_AT);
596  if (is_null($data)) {
597  return null;
598  }
599  $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
600  if (false === $data) {
601  return null;
602  }
603  $this->lastModifiedAt = $data;
604  }
605 
606  return $this->lastModifiedAt;
607  }
608 
609 
613  public function setId(?string $id): void
614  {
615  $this->id = $id;
616  }
617 
621  public function setVersion(?int $version): void
622  {
623  $this->version = $version;
624  }
625 
629  public function setCreatedAt(?DateTimeImmutable $createdAt): void
630  {
631  $this->createdAt = $createdAt;
632  }
633 
637  public function setLastModifiedAt(?DateTimeImmutable $lastModifiedAt): void
638  {
639  $this->lastModifiedAt = $lastModifiedAt;
640  }
641 
642 
643  #[\ReturnTypeWillChange]
644  public function jsonSerialize()
645  {
646  $data = $this->toArray();
647  if (isset($data[BaseResource::FIELD_CREATED_AT]) && $data[BaseResource::FIELD_CREATED_AT] instanceof \DateTimeImmutable) {
648  $data[BaseResource::FIELD_CREATED_AT] = $data[BaseResource::FIELD_CREATED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
649  }
650 
651  if (isset($data[BaseResource::FIELD_LAST_MODIFIED_AT]) && $data[BaseResource::FIELD_LAST_MODIFIED_AT] instanceof \DateTimeImmutable) {
652  $data[BaseResource::FIELD_LAST_MODIFIED_AT] = $data[BaseResource::FIELD_LAST_MODIFIED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
653  }
654  return (object) $data;
655  }
656 }
__construct(?string $id=null, ?int $version=null, ?DateTimeImmutable $createdAt=null, ?DateTimeImmutable $lastModifiedAt=null)
setLastModifiedAt(?DateTimeImmutable $lastModifiedAt)