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
483use DateTimeImmutable;
484use stdClass;
485
489final class BaseResourceModel extends JsonObjectModel implements BaseResource
490{
495 protected $id;
496
501 protected $version;
502
507 protected $createdAt;
508
514
515
519 public function __construct(
520 ?string $id = null,
521 ?int $version = null,
522 ?DateTimeImmutable $createdAt = null,
523 ?DateTimeImmutable $lastModifiedAt = null
524 ) {
525 $this->id = $id;
526 $this->version = $version;
527 $this->createdAt = $createdAt;
528 $this->lastModifiedAt = $lastModifiedAt;
529 }
530
535 public function getId()
536 {
537 if (is_null($this->id)) {
539 $data = $this->raw(self::FIELD_ID);
540 if (is_null($data)) {
541 return null;
542 }
543 $this->id = (string) $data;
544 }
545
546 return $this->id;
547 }
548
553 public function getVersion()
554 {
555 if (is_null($this->version)) {
557 $data = $this->raw(self::FIELD_VERSION);
558 if (is_null($data)) {
559 return null;
560 }
561 $this->version = (int) $data;
562 }
563
564 return $this->version;
565 }
566
571 public function getCreatedAt()
572 {
573 if (is_null($this->createdAt)) {
575 $data = $this->raw(self::FIELD_CREATED_AT);
576 if (is_null($data)) {
577 return null;
578 }
579 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
580 if (false === $data) {
581 return null;
582 }
583 $this->createdAt = $data;
584 }
585
586 return $this->createdAt;
587 }
588
593 public function getLastModifiedAt()
594 {
595 if (is_null($this->lastModifiedAt)) {
597 $data = $this->raw(self::FIELD_LAST_MODIFIED_AT);
598 if (is_null($data)) {
599 return null;
600 }
601 $data = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, $data);
602 if (false === $data) {
603 return null;
604 }
605 $this->lastModifiedAt = $data;
606 }
607
609 }
610
611
615 public function setId(?string $id): void
616 {
617 $this->id = $id;
618 }
619
623 public function setVersion(?int $version): void
624 {
625 $this->version = $version;
626 }
627
631 public function setCreatedAt(?DateTimeImmutable $createdAt): void
632 {
633 $this->createdAt = $createdAt;
634 }
635
639 public function setLastModifiedAt(?DateTimeImmutable $lastModifiedAt): void
640 {
641 $this->lastModifiedAt = $lastModifiedAt;
642 }
643
644
645 #[\ReturnTypeWillChange]
646 public function jsonSerialize()
647 {
648 $data = $this->toArray();
649 if (isset($data[BaseResource::FIELD_CREATED_AT]) && $data[BaseResource::FIELD_CREATED_AT] instanceof \DateTimeImmutable) {
650 $data[BaseResource::FIELD_CREATED_AT] = $data[BaseResource::FIELD_CREATED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
651 }
652
653 if (isset($data[BaseResource::FIELD_LAST_MODIFIED_AT]) && $data[BaseResource::FIELD_LAST_MODIFIED_AT] instanceof \DateTimeImmutable) {
654 $data[BaseResource::FIELD_LAST_MODIFIED_AT] = $data[BaseResource::FIELD_LAST_MODIFIED_AT]->setTimeZone(new \DateTimeZone('UTC'))->format('c');
655 }
656 return (object) $data;
657 }
658}
__construct(?string $id=null, ?int $version=null, ?DateTimeImmutable $createdAt=null, ?DateTimeImmutable $lastModifiedAt=null)
setLastModifiedAt(?DateTimeImmutable $lastModifiedAt)