Skip to content

Commit

Permalink
Fix timeframes REST
Browse files Browse the repository at this point in the history
  • Loading branch information
firstred committed Mar 31, 2021
1 parent edff22a commit e4f254c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Entity/ReasonNoTimeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
use Firstred\PostNL\Service\LocationService;
use Firstred\PostNL\Service\ShippingStatusService;
use Firstred\PostNL\Service\TimeframeService;
use stdClass;
use function array_merge;
use function is_array;
use function is_string;

/**
* Class ReasonNoTimeframe.
Expand Down Expand Up @@ -188,6 +192,46 @@ public function setDate($date = null)
return $this;
}

/**
* @param stdClass $json
*
* @return mixed|stdClass|null
*
* @throws InvalidArgumentException
* @throws \Firstred\PostNL\Exception\NotSupportedException
*
* @since 1.2.0
*/
public static function jsonDeserialize(stdClass $json)
{
if (isset($json->ReasonNoTimeframe->Options)) {
/** @psalm-var list<string> $deliveryOptions */
$deliveryOptions = [];
if (!is_array($json->ReasonNoTimeframe->Options)){
$json->ReasonNoTimeframe->Options = [$json->ReasonNoTimeframe->Options];
}

foreach ($json->ReasonNoTimeframe->Options as $deliveryOption) {
if (isset($deliveryOption->string)) {
if (!is_array($deliveryOption->string)) {
$deliveryOption->string = [$deliveryOption->string];
}
foreach ($deliveryOption->string as $optionString) {
$deliveryOptions[] = $optionString;
}
} elseif (is_array($deliveryOption)) {
$deliveryOptions = array_merge($deliveryOptions, $deliveryOption);
} elseif (is_string($deliveryOption)) {
$deliveryOptions[] = $deliveryOption;
}
}

$json->ReasonNoTimeframe->Options = $deliveryOptions;
}

return parent::jsonDeserialize($json);
}

/**
* Return a serializable array for the XMLWriter.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Entity/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
use Firstred\PostNL\Service\LocationService;
use Firstred\PostNL\Service\ShippingStatusService;
use Firstred\PostNL\Service\TimeframeService;
use stdClass;
use function array_merge;
use function is_array;
use function is_string;

/**
* Class Timeframe.
Expand Down
44 changes: 44 additions & 0 deletions src/Entity/TimeframeTimeFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
use Firstred\PostNL\Service\LocationService;
use Firstred\PostNL\Service\ShippingStatusService;
use Firstred\PostNL\Service\TimeframeService;
use stdClass;
use function array_merge;
use function is_array;
use function is_string;

/**
* Class TimeframeTimeFrame.
Expand Down Expand Up @@ -150,4 +154,44 @@ public function setDate($date = null)

return $this;
}

/**
* @param stdClass $json
*
* @return mixed|stdClass|null
*
* @throws \Firstred\PostNL\Exception\InvalidArgumentException
* @throws \Firstred\PostNL\Exception\NotSupportedException
*
* @since 1.2.0
*/
public static function jsonDeserialize(stdClass $json)
{
if (isset($json->TimeframeTimeFrame->Options)) {
/** @psalm-var list<string> $deliveryOptions */
$deliveryOptions = [];
if (!is_array($json->TimeframeTimeFrame->Options)){
$json->TimeframeTimeFrame->Options = [$json->TimeframeTimeFrame->Options];
}

foreach ($json->TimeframeTimeFrame->Options as $deliveryOption) {
if (isset($deliveryOption->string)) {
if (!is_array($deliveryOption->string)) {
$deliveryOption->string = [$deliveryOption->string];
}
foreach ($deliveryOption->string as $optionString) {
$deliveryOptions[] = $optionString;
}
} elseif (is_array($deliveryOption)) {
$deliveryOptions = array_merge($deliveryOptions, $deliveryOption);
} elseif (is_string($deliveryOption)) {
$deliveryOptions[] = $deliveryOption;
}
}

$json->TimeframeTimeFrame->Options = $deliveryOptions;
}

return parent::jsonDeserialize($json);
}
}

0 comments on commit e4f254c

Please sign in to comment.