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

HP-1928: array => string type #64

Merged
merged 11 commits into from
Jun 5, 2024
31 changes: 26 additions & 5 deletions src/sale/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use hiqdev\php\billing\Exception\InvariantException;
use hiqdev\php\billing\plan\PlanInterface;
use hiqdev\php\billing\target\TargetInterface;
use yii\helpers\Json;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package does not depend on Yii.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary import of yii\helpers\Json.

This package does not depend on Yii, as noted by SilverFire in a previous comment. Please remove this import to avoid confusion and potential errors.


/**
* Sale.
Expand Down Expand Up @@ -52,22 +53,22 @@ class Sale implements SaleInterface

protected ?DateTimeImmutable $closeTime = null;

protected ?array $data = null;
protected ?string $data = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you prefer to store data as a JSON string instead of an array?


public function __construct(
$id,
TargetInterface $target,
CustomerInterface $customer,
?PlanInterface $plan = null,
?DateTimeImmutable $time = null,
?array $data = null,
$data = null,
) {
$this->id = $id;
$this->target = $target;
$this->customer = $customer;
$this->plan = $plan;
$this->time = $time ?? new DateTimeImmutable();
$this->data = $data;
$this->data = $this->setData($data);
}

public function getId()
Expand Down Expand Up @@ -129,9 +130,29 @@ public function setId($id)
$this->id = $id;
}

public function getData()
public function setData($data): self
{
return $this->data;
if (empty($data)) {
$this->data = null;
return $this;
}

if (is_string($data) && json_validate($data)) {
$this->data = $data;
return $this;
}

if (is_array($data)) {
$this->data = Json::encode($data);
return $this;
}

throw new \Exception("Cannot assign data");
}

public function getData(): ?array
{
return !empty($this->data) ? Json::decode($this->data, true) : null;
}

public function jsonSerialize(): array
Expand Down
2 changes: 2 additions & 0 deletions src/sale/SaleCreationDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ class SaleCreationDto
public $closeTime;

public $data;

public $reason;
}
2 changes: 1 addition & 1 deletion src/sale/SaleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SaleFactory implements SaleFactoryInterface
*/
public function create(SaleCreationDto $dto)
{
$sale = new Sale($dto->id, $dto->target, $dto->customer, $dto->plan, $dto->time);
$sale = new Sale($dto->id, $dto->target, $dto->customer, $dto->plan, $dto->time, $dto->data);

if ($dto->closeTime !== null) {
$sale->close($dto->closeTime);
Expand Down
4 changes: 2 additions & 2 deletions src/sale/SaleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public function getTime();
public function getCloseTime(): ?DateTimeImmutable;

/**
* @return string|array|null
* @return array|null
*/
public function getData();
public function getData(): ?array;

/**
* @param DateTimeImmutable $time
Expand Down
26 changes: 19 additions & 7 deletions tests/behat/bootstrap/BillingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ public function setConsumption(string $type, int $amount, string $unit, string $
$this->builder->setConsumption($type, $amount, $unit, $target, $time);
}

/**
* @Given /recalculate autotariff for target (\S+)( +at (\S+))?$/
*/
public function recalculateAutoTariff(string $target, string $time = null): void
{
$this->builder->clientSetAutoTariff($target, $time);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??

/**
* @Given /perform billing at (\S+)/
*/
Expand Down Expand Up @@ -231,14 +239,14 @@ public function billWithTime($type, $sum, $currency, $quantity, $unit, $target,
'quantity' => "$quantity $unit",
'time' => $time,
]);
Assert::assertSame($type, $bill->getType()->getName());
Assert::assertSame($target, $bill->getTarget()->getFullName());
Assert::assertEquals(bcmul($sum, 100), $bill->getSum()->getAmount());
Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode());
Assert::assertEquals((float)$quantity, (float)$bill->getQuantity()->getQuantity());
Assert::assertEquals(strtolower($unit), strtolower($bill->getQuantity()->getUnit()->getName()));
Assert::assertSame($type, $bill->getType()->getName(), "Bill type mismatch: expected $type, got {$bill->getType()->getName()}");
Assert::assertSame($target, $bill->getTarget()->getFullName(), "Bill target mismatch: expected $target, got {$bill->getTarget()->getFullName()}");
Assert::assertEquals(bcmul($sum, 100), $bill->getSum()->getAmount(), "Bill sum mismatch: expected $sum, got {$bill->getSum()->getAmount()}");
Assert::assertSame($currency, $bill->getSum()->getCurrency()->getCode(), "Bill currency mismatch: expected $currency, got {$bill->getSum()->getCurrency()->getCode()}");
Assert::assertEquals((float)$quantity, (float)$bill->getQuantity()->getQuantity(), "Bill quantity mismatch: expected $quantity, got {$bill->getQuantity()->getQuantity()}");
Assert::assertEquals(strtolower($unit), strtolower($bill->getQuantity()->getUnit()->getName()), "Bill unit mismatch: expected $unit, got {$bill->getQuantity()->getUnit()->getName()}");
if ($time) {
Assert::assertEquals(new DateTimeImmutable($time), $bill->getTime());
Assert::assertEquals(new DateTimeImmutable($time), $bill->getTime(), "Bill time mismatch: expected $time, got {$bill->getTime()->format(DATE_ATOM)}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you've mixed different PRs

}
}

Expand Down Expand Up @@ -337,6 +345,10 @@ protected function prepareTime(string $time = null)
if (strncmp($time, 'pY', 1) === 0) {
return date(substr($time, 1), strtotime('-1 year'));
}
if (str_contains($time, 'pm')) {
$time = str_replace('pm', 'm', $time);
$time = date($time, strtotime('-1 month'));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

if (strncmp($time, 'Y', 1) === 0) {
return date($time);
}
Expand Down
Loading