Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Add BeVariantInterface and ProductInterface #620

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Classes/Domain/Model/Cart/AdditionalDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Domain\Model\Cart;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

interface AdditionalDataInterface
{
/**
* @return array<string, mixed>
*/
public function getAdditionals(): array;

/**
* @param array<string, mixed> $additional
*/
public function setAdditionals(array $additional): void;

public function unsetAdditionals(): void;

public function getAdditional(string $key): mixed;

public function setAdditional(string $key, mixed $value): void;

public function unsetAdditional(string $key): void;
}
58 changes: 58 additions & 0 deletions Classes/Domain/Model/Cart/AdditionalDataTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Domain\Model\Cart;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

trait AdditionalDataTrait
{
/**
* @var array<string, mixed>
*/
private array $additionals;

/**
* @return array<string, mixed>
*/
public function getAdditionals(): array
{
return $this->additionals;
}

/**
* @param array<string, mixed> $additionals
*/
public function setAdditionals(array $additionals): void
{
$this->additionals = $additionals;
}

public function unsetAdditionals(): void
{
$this->additionals = [];
}

public function getAdditional(string $key): mixed
{
return $this->additionals[$key];
}

public function setAdditional(string $key, mixed $value): void
{
$this->additionals[$key] = $value;
}

public function unsetAdditional(string $key): void
{
if (isset($this->additionals[$key])) {
unset($this->additionals[$key]);
}
}
}
107 changes: 23 additions & 84 deletions Classes/Domain/Model/Cart/BeVariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
* LICENSE file that was distributed with this source code.
*/

class BeVariant
final class BeVariant implements AdditionalDataInterface, BeVariantInterface
{
use AdditionalDataTrait;

protected string $titleDelimiter = ' - ';

protected string $skuDelimiter = '-';
Expand All @@ -29,19 +31,15 @@ class BeVariant

protected bool $isFeVariant = false;

protected int $hasFeVariants;

protected int $min = 0;

protected int $max = 0;

protected array $additional = [];

protected int $stock = 0;

public function __construct(
protected string $id,
protected self|Product $parent,
protected BeVariantInterface|ProductInterface $parent,
protected string $title,
protected string $sku,
protected int $priceCalcMethod,
Expand All @@ -65,7 +63,7 @@ public function toArray(): array
'price_total_gross' => $this->gross,
'price_total_net' => $this->net,
'tax' => $this->tax,
'additional' => $this->additional,
'additionals' => $this->getAdditionals(),
];

if ($this->beVariants) {
Expand All @@ -81,19 +79,19 @@ public function toArray(): array
return $variantArr;
}

public function getParent(): self|Product
public function getParent(): BeVariantInterface|ProductInterface
{
return $this->parent;
}

public function setParent(self|Product $parent): void
public function setParent(BeVariantInterface|ProductInterface $parent): void
{
$this->parent = $parent;
}

public function getProduct(): Product
public function getProduct(): ProductInterface
{
if ($this->parent instanceof Product) {
if ($this->parent instanceof ProductInterface) {
return $this->parent;
}

Expand Down Expand Up @@ -129,9 +127,9 @@ public function getCompleteTitle(): string
{
$title = '';

if ($this->parent instanceof self) {
if ($this->parent instanceof BeVariantInterface) {
$title = $this->parent->getCompleteTitle();
} elseif ($this->parent instanceof Product) {
} elseif ($this->parent instanceof ProductInterface) {
$title = $this->parent->getTitle();
}

Expand All @@ -149,7 +147,7 @@ public function getCompleteTitleWithoutProduct(): string
$title = '';
$titleDelimiter = '';

if ($this->parent instanceof self) {
if ($this->parent instanceof BeVariantInterface) {
$title = $this->parent->getCompleteTitleWithoutProduct();
$titleDelimiter = $this->titleDelimiter;
}
Expand Down Expand Up @@ -215,9 +213,9 @@ public function getPriceCalculated(): float
{
$price = $this->getBestPrice();

if ($this->parent instanceof self) {
if ($this->parent instanceof BeVariantInterface) {
$parentPrice = $this->parent->getBestPrice();
} elseif ($this->parent instanceof Product) {
} elseif ($this->parent instanceof ProductInterface) {
$parentPrice = $this->parent->getBestPrice($this->getQuantity());
} else {
$parentPrice = 0.0;
Expand Down Expand Up @@ -256,7 +254,7 @@ public function getParentPrice(): float
return 0.0;
}

if ($this->parent instanceof self) {
if ($this->parent instanceof BeVariantInterface) {
return $this->parent->getBestPrice();
}

Expand Down Expand Up @@ -299,17 +297,13 @@ public function getCompleteSku(): string
{
$sku = '';

if ($this->parent instanceof self) {
if ($this->parent instanceof BeVariantInterface) {
$sku = $this->parent->getCompleteSku();
} elseif ($this->parent instanceof Product) {
} elseif ($this->parent instanceof ProductInterface) {
$sku = $this->parent->getSku();
}

if ($this->isFeVariant) {
$sku .= $this->skuDelimiter . $this->id;
} else {
$sku .= $this->skuDelimiter . $this->sku;
}
$sku .= $this->skuDelimiter . $this->sku;

return $sku;
}
Expand All @@ -318,47 +312,15 @@ public function getCompleteSkuWithoutProduct(): string
{
$sku = '';

$skuDelimiter = '';

if ($this->parent instanceof self) {
if ($this->parent instanceof BeVariantInterface) {
$sku = $this->parent->getCompleteSkuWithoutProduct();
$skuDelimiter = $this->titleDelimiter;
}

if ($this->isFeVariant) {
$sku .= $skuDelimiter . $this->id;
} else {
$sku .= $skuDelimiter . $this->sku;
}
$sku .= $this->skuDelimiter . $this->sku;

return $sku;
}

public function setSku(string $sku): void
{
$this->sku = $sku;
}

public function getHasFeVariants(): int
{
return $this->hasFeVariants;
}

public function setHasFeVariants(int $hasFeVariants): void
{
$this->hasFeVariants = $hasFeVariants;
}

public function isFeVariant(): bool
{
return $this->isFeVariant;
}

public function setIsFeVariant(bool $isFeVariant): void
{
$this->isFeVariant = $isFeVariant;
}

public function getQuantity(): int
{
return $this->quantity;
Expand All @@ -381,7 +343,7 @@ public function getTax(): float
return $this->tax;
}

public function getTaxClass(): ?TaxClass
public function getTaxClass(): TaxClass
{
return $this->parent->getTaxClass();
}
Expand Down Expand Up @@ -427,7 +389,7 @@ public function addBeVariants(array $newVariants): void
}
}

public function addBeVariant(self $newBeVariant): void
public function addBeVariant(BeVariantInterface $newBeVariant): void
{
$newBeVariantId = $newBeVariant->getId();

Expand All @@ -452,7 +414,7 @@ public function getBeVariants(): array
return $this->beVariants;
}

public function getBeVariantById(int $beVariantId): ?self
public function getBeVariantById(int $beVariantId): ?BeVariantInterface
{
return $this->beVariants[$beVariantId] ?? null;
}
Expand Down Expand Up @@ -557,29 +519,6 @@ protected function reCalc(): void
}
}

public function getAdditionalArray(): array
{
return $this->additional;
}

public function setAdditionalArray(array $additional): void
{
$this->additional = $additional;
}

/**
* @return mixed
*/
public function getAdditional(string $key)
{
return $this->additional[$key];
}

public function setAdditional(string $key, mixed $value): void
{
$this->additional[$key] = $value;
}

public function getMin(): int
{
return $this->min;
Expand Down
35 changes: 35 additions & 0 deletions Classes/Domain/Model/Cart/BeVariantFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Domain\Model\Cart;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

final readonly class BeVariantFactory implements BeVariantFactoryInterface
{
public function create(
string $id,
BeVariantInterface|ProductInterface $parent,
string $title,
string $sku,
int $priceCalcMethod,
float $price,
int $quantity = 0
): BeVariantInterface {
return new BeVariant(
$id,
$parent,
$title,
$sku,
$priceCalcMethod,
$price,
$quantity,
);
}
}
17 changes: 17 additions & 0 deletions Classes/Domain/Model/Cart/BeVariantFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Domain\Model\Cart;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

interface BeVariantFactoryInterface
{
public function create(string $id, BeVariantInterface|ProductInterface $parent, string $title, string $sku, int $priceCalcMethod, float $price, int $quantity = 0): BeVariantInterface;
}
Loading
Loading