Skip to content

Commit

Permalink
fix(core): fix data that should not be stored being stored (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine authored Oct 27, 2023
1 parent e58b181 commit 77e8c09
Show file tree
Hide file tree
Showing 79 changed files with 469 additions and 574 deletions.
3 changes: 1 addition & 2 deletions private/Types/Shared/Model/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MyParcelNL\Pdk\Console\Types\Shared\Model;

use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Model\Model;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Collection;
Expand All @@ -25,7 +24,7 @@
* @property ReflectionClass $ref
* @property TypeCollection $types
*/
final class ClassDefinition extends Model implements StorableArrayable
final class ClassDefinition extends Model
{
public $attributes = [
'comments' => KeyValueCollection::class,
Expand Down
3 changes: 1 addition & 2 deletions private/Types/Shared/Model/ClassMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MyParcelNL\Pdk\Console\Types\Shared\Model;

use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Model\Model;
use ReflectionMethod;

Expand All @@ -13,7 +12,7 @@
* @property \ReflectionMethod $ref
* @property array $parameters
*/
class ClassMethod extends Model implements StorableArrayable
class ClassMethod extends Model
{
public $attributes = [
'name' => null,
Expand Down
11 changes: 1 addition & 10 deletions src/Account/Model/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace MyParcelNL\Pdk\Account\Model;

use MyParcelNL\Pdk\Account\Collection\ShopCollection;
use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Model\ContactDetails;
use MyParcelNL\Pdk\Base\Model\Model;
use MyParcelNL\Pdk\Base\Support\Collection;
Expand All @@ -19,7 +18,7 @@
* @property int $status
* @property array $subscriptionFeatures
*/
class Account extends Model implements StorableArrayable
class Account extends Model
{
public const FEATURE_ORDER_NOTES = 'allow_order_notes';

Expand All @@ -42,12 +41,4 @@ class Account extends Model implements StorableArrayable
'status' => 'int',
'subscriptionFeatures' => Collection::class,
];

/**
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
public function toStorableArray(): array
{
return $this->toArrayWithoutNull();
}
}
17 changes: 10 additions & 7 deletions src/App/Order/Model/PdkOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderLineCollection;
use MyParcelNL\Pdk\App\Order\Collection\PdkOrderNoteCollection;
use MyParcelNL\Pdk\App\Order\Contract\PdkOrderNoteRepositoryInterface;
use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Model\ContactDetails;
use MyParcelNL\Pdk\Base\Model\Model;
use MyParcelNL\Pdk\Base\Support\Arr;
Expand All @@ -17,7 +16,6 @@
use MyParcelNL\Pdk\Shipment\Collection\ShipmentCollection;
use MyParcelNL\Pdk\Shipment\Model\CustomsDeclaration;
use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions;
use MyParcelNL\Pdk\Shipment\Model\PhysicalProperties;
use MyParcelNL\Pdk\Shipment\Model\Shipment;
use MyParcelNL\Pdk\Validation\Validator\OrderValidator;

Expand All @@ -33,7 +31,7 @@
* @property null|\MyParcelNL\Pdk\Base\Model\ContactDetails $billingAddress
* @property \MyParcelNL\Pdk\App\Order\Model\ShippingAddress $shippingAddress
* @property null|\MyParcelNL\Pdk\Shipment\Collection\ShipmentCollection $shipments
* @property \MyParcelNL\Pdk\Shipment\Model\PhysicalProperties $physicalProperties
* @property \MyParcelNL\Pdk\App\Order\Model\PdkPhysicalProperties $physicalProperties
* @property null|\DateTimeImmutable $orderDate
* @property bool $exported
* @property int $shipmentPrice
Expand All @@ -46,7 +44,7 @@
* @property int $totalPriceAfterVat
* @property int $totalVat
*/
class PdkOrder extends Model implements StorableArrayable
class PdkOrder extends Model
{
protected $attributes = [
/** Plugin order id */
Expand All @@ -69,7 +67,7 @@ class PdkOrder extends Model implements StorableArrayable
*/
'shipments' => ShipmentCollection::class,
'customsDeclaration' => null,
'physicalProperties' => PhysicalProperties::class,
'physicalProperties' => PdkPhysicalProperties::class,
'lines' => PdkOrderLineCollection::class,
'notes' => null,

Expand Down Expand Up @@ -108,7 +106,7 @@ class PdkOrder extends Model implements StorableArrayable

'shipments' => ShipmentCollection::class,
'customsDeclaration' => CustomsDeclaration::class,
'physicalProperties' => PhysicalProperties::class,
'physicalProperties' => PdkPhysicalProperties::class,
'lines' => PdkOrderLineCollection::class,
'notes' => PdkOrderNoteCollection::class,

Expand Down Expand Up @@ -192,7 +190,12 @@ public function createShipment(): Shipment
'carrier' => $deliveryOptions->carrier,
'orderId' => $this->externalIdentifier,
'dropOffPoint' => null,
'physicalProperties' => $this->physicalProperties,
'physicalProperties' => [
'height' => $this->physicalProperties->height,
'length' => $this->physicalProperties->length,
'width' => $this->physicalProperties->width,
'weight' => $this->physicalProperties->totalWeight,
],
]);
}

Expand Down
12 changes: 1 addition & 11 deletions src/App/Order/Model/PdkOrderNote.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MyParcelNL\Pdk\App\Order\Model;

use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Model\Model;
use MyParcelNL\Pdk\Fulfilment\Model\OrderNote;

Expand All @@ -17,7 +16,7 @@
* @property null|\DateTime $createdAt
* @property null|\DateTime $updatedAt
*/
class PdkOrderNote extends Model implements StorableArrayable
class PdkOrderNote extends Model
{
public $attributes = [
/**
Expand Down Expand Up @@ -67,13 +66,4 @@ public static function fromFulfilmentOrderNote(OrderNote $orderNote): self
'updatedAt' => $orderNote->updatedAt,
]);
}

/**
* @return array
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
public function toStorableArray(): array
{
return $this->toArrayWithoutNull();
}
}
73 changes: 73 additions & 0 deletions src/App/Order/Model/PdkPhysicalProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Pdk\App\Order\Model;

use MyParcelNL\Pdk\Base\Model\Model;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\Pdk\Types\Service\TriStateService;

/**
* @property null|int $height
* @property null|int $length
* @property null|int $width
* @property int $initialWeight
* @property int $manualWeight
* @property int $totalWeight
*/
class PdkPhysicalProperties extends Model
{
protected $attributes = [
'height' => null,
'length' => null,
'width' => null,

/**
* Base weight.
*/
'initialWeight' => 0,

/**
* Optional manual override of the initial weight.
*/
'manualWeight' => TriStateService::INHERIT,

/**
* Calculated automatically based on the initial weight and manual weight.
*/
'totalWeight' => 0,
];

protected $casts = [
'height' => 'int',
'length' => 'int',
'width' => 'int',
'initialWeight' => 'int',
'manualWeight' => 'int',
'totalWeight' => 'int',
];

/**
* @return array
*/
public function toStorableArray(): array
{
return Utils::filterNull([
'manualWeight' => TriStateService::INHERIT === $this->manualWeight ? null : $this->manualWeight,
]);
}

/**
* @return int
* @noinspection PhpUnused
*/
protected function getTotalWeightAttribute(): int
{
/** @var TriStateService $triStateService */
$triStateService = Pdk::get(TriStateService::class);

return $triStateService->resolve($this->manualWeight, $this->initialWeight);
}
}
3 changes: 1 addition & 2 deletions src/App/Order/Model/PdkProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace MyParcelNL\Pdk\App\Order\Model;

use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Model\Currency;
use MyParcelNL\Pdk\Base\Model\Model;
use MyParcelNL\Pdk\Facade\Pdk;
Expand All @@ -27,7 +26,7 @@
* @property \MyParcelNL\Pdk\Settings\Model\ProductSettings $mergedSettings
* @property null|\MyParcelNL\Pdk\App\Order\Model\PdkProduct $parent
*/
class PdkProduct extends Model implements StorableArrayable
class PdkProduct extends Model
{
/**
* @var array
Expand Down
5 changes: 4 additions & 1 deletion src/Base/Concern/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use DateTimeInterface;
use DateTimeZone;
use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Exception\InvalidCastException;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Base\Support\Collection;
Expand Down Expand Up @@ -327,7 +328,9 @@ protected function addCastAttributesToArray(array $attributes, array $mutatedAtt
$attributes[$key] = $this->serializeDate($attributes[$key]);
}

if ($attributes[$key] instanceof Arrayable) {
if ($flags & Arrayable::STORABLE && $attributes[$key] instanceof StorableArrayable) {
$attributes[$key] = $attributes[$key]->toStorableArray();
} elseif ($attributes[$key] instanceof Arrayable) {
$attributes[$key] = $attributes[$key]->toArray($flags);
}

Expand Down
4 changes: 4 additions & 0 deletions src/Base/Contract/Arrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface Arrayable
public const CASE_KEBAB = 2;
public const CASE_STUDLY = 4;
public const SKIP_NULL = 8;
public const STORABLE = 16;
// Combinations
public const STORABLE_NULL = self::STORABLE | self::SKIP_NULL;
public const ENCODED = self::SKIP_NULL | self::CASE_SNAKE;

/**
* Get the instance as an array.
Expand Down
12 changes: 11 additions & 1 deletion src/Base/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use MyParcelNL\Pdk\Base\Concern\HasAttributes;
use MyParcelNL\Pdk\Base\Contract\Arrayable;
use MyParcelNL\Pdk\Base\Contract\ModelInterface;
use MyParcelNL\Pdk\Base\Contract\StorableArrayable;
use MyParcelNL\Pdk\Base\Support\Utils;
use MyParcelNL\Sdk\src\Support\Str;

/**
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
*/
class Model implements Arrayable, ArrayAccess, ModelInterface
class Model implements StorableArrayable, ArrayAccess, ModelInterface
{
use HasAttributes;

Expand Down Expand Up @@ -231,6 +232,15 @@ public function toSnakeCaseArray(): array
return $this->toArray(Arrayable::CASE_SNAKE);
}

/**
* @return array
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
*/
public function toStorableArray(): array
{
return $this->toArray(Arrayable::STORABLE_NULL);
}

/**
* @return array
* @throws \MyParcelNL\Pdk\Base\Exception\InvalidCastException
Expand Down
10 changes: 10 additions & 0 deletions src/Carrier/Model/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,14 @@ public function getTypeAttribute(): string
{
return $this->subscriptionId ? self::TYPE_CUSTOM : self::TYPE_MAIN;
}

/**
* @return string[]
*/
public function toStorableArray(): array
{
return [
'externalIdentifier' => $this->externalIdentifier,
];
}
}
2 changes: 1 addition & 1 deletion src/Context/Model/OrderDataContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @property null|\MyParcelNL\Pdk\Base\Model\ContactDetails $billingAddress
* @property \MyParcelNL\Pdk\App\Order\Model\ShippingAddress $shippingAddress
* @property null|\MyParcelNL\Pdk\Shipment\Collection\ShipmentCollection $shipments
* @property \MyParcelNL\Pdk\Shipment\Model\PhysicalProperties $physicalProperties
* @property \MyParcelNL\Pdk\App\Order\Model\PdkPhysicalProperties $physicalProperties
* @property null|\DateTimeImmutable $orderDate
* @property bool $exported
* @property int $shipmentPrice
Expand Down
9 changes: 4 additions & 5 deletions src/Fulfilment/Model/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ public static function fromPdkShipment(?PdkShipment $pdkShipment): self
'recipient' => $pdkShipment->recipient,
'dropOffPoint' => $pdkShipment->dropOffPoint,
'physicalProperties' => [
'height' => $pdkShipment->physicalProperties->height ?? 0,
'width' => $pdkShipment->physicalProperties->width ?? 0,
'length' => $pdkShipment->physicalProperties->length ?? 0,
'initialWeight' => $pdkShipment->physicalProperties->initialWeight,
'manualWeight' => $pdkShipment->physicalProperties->manualWeight,
'height' => $pdkShipment->physicalProperties->height ?? 0,
'width' => $pdkShipment->physicalProperties->width ?? 0,
'length' => $pdkShipment->physicalProperties->length ?? 0,
'weight' => $pdkShipment->physicalProperties->weight ?? 0,
],
]);
}
Expand Down
17 changes: 6 additions & 11 deletions src/Fulfilment/Request/PostOrdersRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function encodeOrder(Order $order): array
: null,
'order_lines' => $order->lines->reduce(
function (array $carry, OrderLine $orderLine) {
$carry[] = $orderLine->except('vat', Arrayable::CASE_SNAKE);
$carry[] = $orderLine->except('vat', Arrayable::ENCODED);

return $carry;
},
Expand Down Expand Up @@ -126,20 +126,15 @@ private function getShipment(Order $order): array
return [
'carrier' => $shipment->carrier,
'customs_declaration' => $shipment->customsDeclaration
? $shipment->customsDeclaration->toSnakeCaseArray()
? $shipment->customsDeclaration->toArray(Arrayable::ENCODED)
: null,
'drop_off_point' => $shipment->dropOffPoint
? $shipment->dropOffPoint->toSnakeCaseArray()
? $shipment->dropOffPoint->toArray(Arrayable::ENCODED)
: null,
'options' => $this->getShipmentOptions($shipment),
'physical_properties' => Utils::filterNull([
'height' => $shipment->physicalProperties->height,
'length' => $shipment->physicalProperties->length,
'width' => $shipment->physicalProperties->width,
'weight' => $shipment->physicalProperties->totalWeight,
]),
'physical_properties' => $shipment->physicalProperties->toArray(Arrayable::ENCODED),
'pickup' => $shipment->pickup
? $shipment->pickup->toSnakeCaseArray()
? $shipment->pickup->toArray(Arrayable::ENCODED)
: null,
'recipient' => $this->getAddress($shipment->recipient),
];
Expand All @@ -153,7 +148,7 @@ private function getShipment(Order $order): array
*/
private function getShipmentOptions(Shipment $shipment): array
{
$options = $shipment->options->toArray(Arrayable::CASE_SNAKE | Arrayable::SKIP_NULL);
$options = $shipment->options->toArray(Arrayable::ENCODED);

$options['insurance'] = [
'amount' => $shipment->options->insurance,
Expand Down
Loading

0 comments on commit 77e8c09

Please sign in to comment.