Skip to content

Commit

Permalink
Merge pull request #46 from cbs-software/return-null-for-empty-dates-…
Browse files Browse the repository at this point in the history
…in-xml

Return null for empty dates in xml
  • Loading branch information
nickdawsontcs authored Jul 1, 2024
2 parents 9b0cd5e + 2af1048 commit 89e1940
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 34 deletions.
31 changes: 22 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use CBS\SmarterU\Queries\ListGroupsQuery;
use CBS\SmarterU\Queries\ListUsersQuery;
use DateTime;
use DateTimeInterface;
use DateTimeImmutable;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\ClientException;
use Psr\Log\LoggerAwareTrait;
Expand Down Expand Up @@ -1039,8 +1041,8 @@ public function getLearnerReport(GetLearnerReportQuery $query): array {
->setGivenName((string) $report->FirstName)
->setLearningModuleId((string) $report->LearningModuleID)
->setUserId((string) $report->UserID)
->setCreatedDate(new DateTime((string) $report->CreatedDate))
->setModifiedDate(new DateTime((string)$report->ModifiedDate));
->setCreatedDate($this->toDateTimeInterface((string)$report->CreatedDate))
->setModifiedDate($this->toDateTimeInterface((string)$report->ModifiedDate));

// Other values may or may not be returned, depending on the input.
if (isset($report->AlternateEmail)) {
Expand All @@ -1050,7 +1052,7 @@ public function getLearnerReport(GetLearnerReportQuery $query): array {
}
if (isset($report->CompletedDate)) {
$currentReport->setCompletedDate(
new DateTime((string) $report->CompletedDate)
$this->toDateTimeInterface((string) $report->CompletedDate)
);
}
if (isset($report->CourseDuration)) {
Expand All @@ -1068,7 +1070,7 @@ public function getLearnerReport(GetLearnerReportQuery $query): array {
}
if (isset($report->DueDate)) {
$currentReport->setDueDate(
new DateTime((string) $report->DueDate)
$this->toDateTimeInterface((string) $report->DueDate)
);
}
if (isset($report->EmployeeID)) {
Expand All @@ -1078,7 +1080,7 @@ public function getLearnerReport(GetLearnerReportQuery $query): array {
}
if (isset($report->EnrolledDate)) {
$currentReport->setEnrolledDate(
new DateTime((string) $report->EnrolledDate)
$this->toDateTimeInterface((string) $report->EnrolledDate)
);
}
if (isset($report->Grade)) {
Expand All @@ -1097,7 +1099,7 @@ public function getLearnerReport(GetLearnerReportQuery $query): array {
}
if (isset($report->LastAccessedDate)) {
$currentReport->setLastAccessedDate(
new DateTime((string) $report->LastAccessedDate)
$this->toDateTimeInterface((string) $report->LastAccessedDate)
);
}
if (isset($report->Points)) {
Expand All @@ -1111,7 +1113,7 @@ public function getLearnerReport(GetLearnerReportQuery $query): array {
}
if (isset($report->StartedDate)) {
$currentReport->setStartedDate(
new DateTime((string) $report->StartedDate)
$this->toDateTimeInterface((string) $report->StartedDate)
);
}
if (isset($report->SubscriptionName)) {
Expand All @@ -1127,15 +1129,15 @@ public function getLearnerReport(GetLearnerReportQuery $query): array {
}
if (isset($report->VariantEndDate)) {
$currentReport->setVariantEndDate(
new DateTime((string) $report->VariantEndDate)
$this->toDateTimeInterface((string) $report->VariantEndDate)
);
}
if (isset($report->VariantName)) {
$currentReport->setVariantName((string) $report->VariantName);
}
if (isset($report->VariantStartDate)) {
$currentReport->setVariantStartDate(
new DateTime((string) $report->VariantStartDate)
$this->toDateTimeInterface((string) $report->VariantStartDate)
);
}
$learnerReports[] = $currentReport;
Expand Down Expand Up @@ -1507,6 +1509,17 @@ private function getErrorCodesFromXmlElement(SimpleXMLElement $errors): array {
return $errorCodes;
}

/**
* Sets the provided date to either null or a DateTimeInterface.
*/
private function toDateTimeInterface(string $dateString): DateTimeInterface|null {
if ($dateString === '') {
return null;
} else {
return new DateTimeImmutable((string) $dateString);
}
}

/**
* Accepts a request XML string and sanitizes it for logging purposes.
*
Expand Down
30 changes: 15 additions & 15 deletions src/DataTypes/LearnerReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ class LearnerReport {
/**
* The UTC date the enrollment was created.
*/
protected DateTimeInterface $createdDate;
protected ?DateTimeInterface $createdDate = null;

/**
* The UTC date the enrollment was last updated.
*/
protected DateTimeInterface $modifiedDate;
protected ?DateTimeInterface $modifiedDate = null;

/**
* The UTC date the User was enrolled in the Course.
Expand Down Expand Up @@ -489,9 +489,9 @@ public function setCourseSessionId(string $courseSessionId): self {
/**
* Get the UTC date the enrollment was created.
*
* @return DateTimeInterface The UTC date the enrollment was created.
* @return ?DateTimeInterface The UTC date the enrollment was created.
*/
public function getCreatedDate(): DateTimeInterface {
public function getCreatedDate(): ?DateTimeInterface {
return $this->createdDate;
}

Expand All @@ -502,17 +502,17 @@ public function getCreatedDate(): DateTimeInterface {
* created.
* @return self
*/
public function setCreatedDate(DateTimeInterface $createdDate): self {
public function setCreatedDate(?DateTimeInterface $createdDate): self {
$this->createdDate = $createdDate;
return $this;
}

/**
* Get the UTC date the enrollment was last updated.
*
* @return DateTimeInterface The UTC date the enrollment was last updated.
* @return ?DateTimeInterface The UTC date the enrollment was last updated.
*/
public function getModifiedDate(): DateTimeInterface {
public function getModifiedDate(): ?DateTimeInterface {
return $this->modifiedDate;
}

Expand All @@ -523,7 +523,7 @@ public function getModifiedDate(): DateTimeInterface {
* last updated.
* @return self
*/
public function setModifiedDate(DateTimeInterface $modifiedDate): self {
public function setModifiedDate(?DateTimeInterface $modifiedDate): self {
$this->modifiedDate = $modifiedDate;
return $this;
}
Expand Down Expand Up @@ -590,7 +590,7 @@ public function getEnrolledDate(): ?DateTimeInterface {
* @param DateTimeInterface $enrolledDate The UTC date the User was enrolled.
* @return self
*/
public function setEnrolledDate(DateTimeInterface $enrolledDate): self {
public function setEnrolledDate(?DateTimeInterface $enrolledDate): self {
$this->enrolledDate = $enrolledDate;
return $this;
}
Expand All @@ -610,7 +610,7 @@ public function getDueDate(): ?DateTimeInterface {
* @param DateTimeInterface $dueDate The UTC date the Course is due.
* @return self
*/
public function setDueDate(DateTimeInterface $dueDate): self {
public function setDueDate(?DateTimeInterface $dueDate): self {
$this->dueDate = $dueDate;
return $this;
}
Expand All @@ -630,7 +630,7 @@ public function getStartedDate(): ?DateTimeInterface {
* @param DateTimeInterface $startedDate The UTC date the User started the Course.
* @return self
*/
public function setStartedDate(DateTimeInterface $startedDate): self {
public function setStartedDate(?DateTimeInterface $startedDate): self {
$this->startedDate = $startedDate;
return $this;
}
Expand All @@ -651,7 +651,7 @@ public function getLastAccessedDate(): ?DateTimeInterface {
* accessed the Course.
* @return self
*/
public function setLastAccessedDate(DateTimeInterface $lastAccessedDate): self {
public function setLastAccessedDate(?DateTimeInterface $lastAccessedDate): self {
$this->lastAccessedDate = $lastAccessedDate;
return $this;
}
Expand All @@ -672,7 +672,7 @@ public function getCompletedDate(): ?DateTimeInterface {
* the Course.
* @return self
*/
public function setCompletedDate(DateTimeInterface $completedDate): self {
public function setCompletedDate(?DateTimeInterface $completedDate): self {
$this->completedDate = $completedDate;
return $this;
}
Expand Down Expand Up @@ -813,7 +813,7 @@ public function getVariantStartDate(): ?DateTimeInterface {
* variant started.
* @return self
*/
public function setVariantStartDate(DateTimeInterface $variantStartDate): self {
public function setVariantStartDate(?DateTimeInterface $variantStartDate): self {
$this->variantStartDate = $variantStartDate;
return $this;
}
Expand All @@ -834,7 +834,7 @@ public function getVariantEndDate(): ?DateTimeInterface {
* variant ends.
* @return self
*/
public function setVariantEndDate(DateTimeInterface $variantEndDate): self {
public function setVariantEndDate(?DateTimeInterface $variantEndDate): self {
$this->variantEndDate = $variantEndDate;
return $this;
}
Expand Down
Loading

0 comments on commit 89e1940

Please sign in to comment.