Skip to content

Commit

Permalink
fix: fix attributes with mutator being omitted when skipping null
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Oct 8, 2024
1 parent 9cf0ad5 commit 3efa2cd
Show file tree
Hide file tree
Showing 37 changed files with 233 additions and 43 deletions.
6 changes: 5 additions & 1 deletion src/App/Audit/Concern/HasAudits.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public function addAudit(string $action, ?string $type = null, ?array $arguments
*/
protected function getAuditsAttribute(): AuditCollection
{
return Audits::allByModel(static::class, $this->getAttribute($this->auditIdentifier));
$identifier = $this->getAttribute($this->auditIdentifier);

return $identifier
? Audits::allByModel(static::class, $identifier)
: new AuditCollection();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/App/Order/Model/PdkOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function createShipment(bool $store = true): Shipment
public function getNotesAttribute(): PdkOrderNoteCollection
{
if (isset($this->attributes['notes'])) {
return $this->getCastAttribute('notes');
return $this->getCastAttributeValue('notes');
}

/** @var \MyParcelNL\Pdk\App\Order\Contract\PdkOrderNoteRepositoryInterface $orderNoteRepository */
Expand Down
13 changes: 8 additions & 5 deletions src/Base/Concern/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ public function getAttributes(?int $flags = null): array

if ($flags) {
if ($flags & Arrayable::SKIP_NULL) {
$attributes = array_filter($attributes, static function ($value) {
return null !== $value;
$attributes = Arr::where($attributes, function ($value, $key) {
return null !== $value || $this->hasGetMutator($key);
});
}

Expand Down Expand Up @@ -536,9 +536,9 @@ protected function createArrayFromAttributes(array $attributes, ?int $flags): ar
{
$mutatedAttributes = $this->getMutatedAttributes();

$attributes = $this->addMutatedAttributesToArray($attributes, $mutatedAttributes, $flags);
$addedAttributes = $this->addMutatedAttributesToArray($attributes, $mutatedAttributes, $flags);

return $this->addCastAttributesToArray($attributes, $mutatedAttributes, $flags);
return $this->addCastAttributesToArray($addedAttributes, $mutatedAttributes, $flags);
}

/**
Expand Down Expand Up @@ -566,8 +566,11 @@ protected function getAttributeValue(string $key)
}

/**
* @param string $string
*
* @return mixed
*/
protected function getCastAttribute(string $string)
protected function getCastAttributeValue(string $string)
{
return $this->castAttribute($string, $this->attributes[$string]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Context/Model/OrderDataContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ protected function getInheritedDeliveryOptionsAttribute(): Collection

$clonedOrder->deliveryOptions->carrier = $newCarrier;

$clonedOrder = $service->calculateShipmentOptions(
$calculatedOrder = $service->calculateShipmentOptions(
$clonedOrder,
PdkOrderOptionsServiceInterface::EXCLUDE_SHIPMENT_OPTIONS
);

$clonedOrder->deliveryOptions->offsetUnset('carrier');
$calculatedOrder->deliveryOptions->offsetUnset('carrier');

return [$carrier->externalIdentifier => $clonedOrder->deliveryOptions->toArrayWithoutNull()];
return [$carrier->externalIdentifier => $calculatedOrder->deliveryOptions->toArrayWithoutNull()];
});
}
}
3 changes: 3 additions & 0 deletions src/Settings/Model/ProductSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* @property int<-1|0|1> $exportOnlyRecipient
* @property int<-1|0|1> $exportReturn
* @property int<-1|0|1> $exportSignature
* @property int<-1|0|1> $exportTracked
* @property int $fitInDigitalStamp
* @property int $fitInMailbox
* @property int<-1>|string $packageType
Expand Down Expand Up @@ -55,6 +56,7 @@ class ProductSettings extends AbstractSettingsModel
self::EXPORT_ONLY_RECIPIENT => TriStateService::INHERIT,
self::EXPORT_RETURN => TriStateService::INHERIT,
self::EXPORT_SIGNATURE => TriStateService::INHERIT,
self::EXPORT_TRACKED => TriStateService::INHERIT,
self::FIT_IN_DIGITAL_STAMP => TriStateService::INHERIT,
self::FIT_IN_MAILBOX => TriStateService::INHERIT,
self::PACKAGE_TYPE => TriStateService::INHERIT,
Expand All @@ -72,6 +74,7 @@ class ProductSettings extends AbstractSettingsModel
self::EXPORT_ONLY_RECIPIENT => TriStateService::TYPE_STRICT,
self::EXPORT_RETURN => TriStateService::TYPE_STRICT,
self::EXPORT_SIGNATURE => TriStateService::TYPE_STRICT,
self::EXPORT_TRACKED => TriStateService::TYPE_STRICT,
self::FIT_IN_DIGITAL_STAMP => 'int',
self::FIT_IN_MAILBOX => 'int',
self::PACKAGE_TYPE => TriStateService::TYPE_COERCED,
Expand Down
2 changes: 1 addition & 1 deletion src/Shipment/Model/DeliveryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function getDateAsString(): ?string
*/
public function getDateAttribute(): ?DateTimeInterface
{
$date = $this->getCastAttribute(self::DATE);
$date = $this->getCastAttributeValue(self::DATE);

if (! $date || $date < new DateTime('now')) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{
"externalIdentifier": "postnl",
"id": 1,
"name": "postnl",
"human": "PostNL",
Expand Down Expand Up @@ -32,6 +33,7 @@
}
},
{
"externalIdentifier": "bpost",
"id": 2,
"name": "bpost",
"human": "bpost",
Expand All @@ -55,6 +57,7 @@
}
},
{
"externalIdentifier": "dpd",
"id": 4,
"name": "dpd",
"human": "DPD",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{
"externalIdentifier": "postnl",
"id": 1,
"name": "postnl",
"human": "PostNL",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{
"externalIdentifier": "postnl",
"id": 1,
"name": "postnl",
"human": "PostNL",
Expand Down Expand Up @@ -44,6 +45,7 @@
}
},
{
"externalIdentifier": "dpd",
"id": 4,
"name": "dpd",
"human": "DPD",
Expand All @@ -67,6 +69,7 @@
}
},
{
"externalIdentifier": "ups",
"id": 8,
"name": "ups",
"human": "UPS",
Expand All @@ -89,6 +92,7 @@
}
},
{
"externalIdentifier": "dhlforyou",
"id": 9,
"name": "dhlforyou",
"human": "DHL For You",
Expand Down Expand Up @@ -133,6 +137,7 @@
}
},
{
"externalIdentifier": "dhlparcelconnect",
"id": 10,
"name": "dhlparcelconnect",
"human": "DHL Parcel Connect",
Expand Down Expand Up @@ -176,6 +181,7 @@
}
},
{
"externalIdentifier": "dhleuroplus",
"id": 11,
"name": "dhleuroplus",
"human": "DHL Europlus",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@
"totalPrice": 0,
"totalVat": 0,
"totalPriceAfterVat": 0,
"inheritedDeliveryOptions": {
"postnl": {
"labelAmount": 1,
"shipmentOptions": {
"insurance": 0,
"ageCheck": 0,
"hideSender": 0,
"largeFormat": 0,
"onlyRecipient": 0,
"return": 0,
"sameDayDelivery": 0,
"signature": 0,
"tracked": 0
},
"deliveryType": "standard",
"packageType": "package"
}
},
"digitalStampRanges": [
{
"min": 0,
Expand Down Expand Up @@ -195,6 +213,24 @@
"totalPrice": 0,
"totalVat": 0,
"totalPriceAfterVat": 0,
"inheritedDeliveryOptions": {
"postnl": {
"labelAmount": 1,
"shipmentOptions": {
"insurance": 0,
"ageCheck": 0,
"hideSender": 0,
"largeFormat": 0,
"onlyRecipient": 0,
"return": 0,
"sameDayDelivery": 0,
"signature": 0,
"tracked": 0
},
"deliveryType": "standard",
"packageType": "package"
}
},
"digitalStampRanges": [
{
"min": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
Expand All @@ -37,6 +38,7 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
Expand All @@ -62,6 +64,7 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
Expand All @@ -79,6 +82,7 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@
"totalPrice": 0,
"totalVat": 0,
"totalPriceAfterVat": 0,
"inheritedDeliveryOptions": {
"postnl": {
"labelAmount": 1,
"shipmentOptions": {
"insurance": 0,
"ageCheck": 0,
"hideSender": 0,
"largeFormat": 0,
"onlyRecipient": 0,
"return": 0,
"sameDayDelivery": 0,
"signature": 0,
"tracked": 0
},
"deliveryType": "standard",
"packageType": "package"
}
},
"digitalStampRanges": [
{
"min": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
Expand All @@ -37,6 +38,7 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"externalIdentifier": "CV515676839NL",
"barcode": "CV515676839NL",
"carrier": {
"externalIdentifier": "postnl",
"id": 1,
"name": "postnl",
"human": "PostNL",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"externalIdentifier": "701",
"deliveryOptions": {
"carrier": {
"externalIdentifier": "postnl",
"id": 1,
"name": "postnl",
"human": "PostNL",
Expand Down Expand Up @@ -230,6 +231,7 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
Expand All @@ -247,13 +249,15 @@
"exportOnlyRecipient": -1,
"exportReturn": -1,
"exportSignature": -1,
"exportTracked": -1,
"fitInDigitalStamp": -1,
"fitInMailbox": -1,
"packageType": -1
}
}
}
],
"notes": [],
"orderDate": "2030-01-01 12:00:00",
"exported": false,
"shipmentPrice": 100,
Expand All @@ -264,12 +268,14 @@
"orderVat": 0,
"totalPrice": 100,
"totalVat": 21,
"totalPriceAfterVat": 121
"totalPriceAfterVat": 121,
"audits": []
},
{
"externalIdentifier": "247",
"deliveryOptions": {
"carrier": {
"externalIdentifier": "postnl",
"id": 1,
"name": "postnl",
"human": "PostNL",
Expand Down Expand Up @@ -340,6 +346,7 @@
"totalWeight": 0
},
"lines": [],
"notes": [],
"exported": false,
"shipmentPrice": 0,
"shipmentPriceAfterVat": 0,
Expand All @@ -349,7 +356,8 @@
"orderVat": 0,
"totalPrice": 0,
"totalVat": 0,
"totalPriceAfterVat": 0
"totalPriceAfterVat": 0,
"audits": []
}
]
}
Expand Down
Loading

0 comments on commit 3efa2cd

Please sign in to comment.