Skip to content
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
43 changes: 39 additions & 4 deletions src/price/AbstractPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class AbstractPrice implements PriceInterface, ChargeModifier
protected $plan;

public function __construct(
$id,
$id,
TypeInterface $type,
TargetInterface $target,
PlanInterface $plan = null
Expand Down Expand Up @@ -137,10 +137,45 @@ public function calculateSum(QuantityInterface $quantity): ?Money

public function jsonSerialize(): array
{
$res = array_filter(get_object_vars($this));
unset($res['plan']);
$data = [
'id' => $this->id,
'class' => (new \ReflectionClass($this))->getShortName(),
'type' => $this->getType()->getName(),
'type_id' => $this->getType()->getId(),
'object_id' => $this->getTarget()->getId(),
'object' => [
'id' => $this->getTarget()->getId(),
'name' => $this->getTarget()->getName(),
'type' => $this->getTarget()->getType(),
],
'plan_id' => $this->getPlan()?->getId(),
];

if ($this instanceof PriceWithSubpriceInterface) {
$data['subprice'] = $this->getSubprices()->values();
}

if ($this instanceof PriceWithRateInterface) {
$data['rate'] = $this->getRate();
}

if ($this instanceof PriceWithUnitInterface) {
$data['unit'] = $this->getUnit()->getName();
}

if ($this instanceof PriceWithCurrencyInterface) {
$data['currency'] = $this->getCurrency()->getCode();
}

if ($this instanceof PriceWithSumsInterface) {
$data['sums'] = $this->getSums()->values();
}

if ($this instanceof PriceWithQuantityInterface) {
$data['quantity'] = $this->getPrepaid()->getQuantity();
}

return $res;
return $data;
}

/**
Expand Down
29 changes: 10 additions & 19 deletions src/price/EnumPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,40 @@
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class EnumPrice extends AbstractPrice
class EnumPrice extends AbstractPrice implements PriceWithSumsInterface, PriceWithCurrencyInterface, PriceWithUnitInterface
{
/**
* @var UnitInterface
*/
protected $unit;
protected UnitInterface $unit;

/**
* @var Currency
*/
protected $currency;
protected Currency $currency;

/**
* @var array quantity => total sum for the quantity
*/
protected $sums;
protected Sums $sums;

public function __construct(
$id,
$id,
TypeInterface $type,
TargetInterface $target,
?PlanInterface $plan,
UnitInterface $unit,
Currency $currency,
array $sums
Sums $sums,
) {
parent::__construct($id, $type, $target, $plan);
$this->unit = $unit;
$this->currency = $currency;
$this->sums = $sums;
}

public function getUnit()
public function getUnit(): UnitInterface
{
return $this->unit;
}

public function getCurrency()
public function getCurrency(): Currency
{
return $this->currency;
}

public function getSums()
public function getSums(): Sums
{
return $this->sums;
}
Expand All @@ -80,7 +71,7 @@ public function calculateSum(QuantityInterface $quantity): ?Money
{
$usage = $this->calculateUsage($quantity)->getQuantity();

foreach ($this->sums as $value => $price) {
foreach ($this->sums->values() as $value => $price) {
if ((string) $value === (string) $usage) {
return new Money($price, $this->currency);
}
Expand Down
21 changes: 21 additions & 0 deletions src/price/HasMoney.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace hiqdev\php\billing\price;

use Money\Currency;
use Money\Money;

trait HasMoney
{
protected Money $price;

public function getPrice(): Money
{
return $this->price;
}

public function getCurrency(): Currency
{
return $this->price->getCurrency();
}
}
25 changes: 25 additions & 0 deletions src/price/HasQuantity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

use hiqdev\php\units\QuantityInterface;
use hiqdev\php\units\UnitInterface;

trait HasQuantity
{
/**
* @var QuantityInterface prepaid quantity also implies Unit
* XXX cannot be null cause Unit is required
*/
protected QuantityInterface $prepaid;

public function getPrepaid(): QuantityInterface
{
return $this->prepaid;
}

public function getUnit(): UnitInterface
{
return $this->prepaid->getUnit();
}
}
4 changes: 2 additions & 2 deletions src/price/PriceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public function findMethodForClass($class)
throw new FailedCreatePriceException("unknown class: $class");
}

public function createEnumPrice(PriceCreationDto $dto)
public function createEnumPrice(PriceCreationDto $dto): EnumPrice
{
return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, $dto->sums);
return new EnumPrice($dto->id, $dto->type, $dto->target, $dto->plan, $dto->unit, $dto->currency, new Sums($dto->sums));
}

public function createRatePrice(PriceCreationDto $dto)
Expand Down
9 changes: 9 additions & 0 deletions src/price/PriceInvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

use hiapi\exceptions\HiapiException;

class PriceInvalidArgumentException extends HiapiException
{
}
10 changes: 10 additions & 0 deletions src/price/PriceWithCurrencyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

use Money\Currency;

interface PriceWithCurrencyInterface
{
public function getCurrency(): Currency;
}
10 changes: 10 additions & 0 deletions src/price/PriceWithMoneyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

use Money\Money;

interface PriceWithMoneyInterface extends PriceWithCurrencyInterface
{
public function getPrice(): Money;
}
10 changes: 10 additions & 0 deletions src/price/PriceWithQuantityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

use hiqdev\php\units\QuantityInterface;

interface PriceWithQuantityInterface extends PriceWithUnitInterface
{
public function getPrepaid(): QuantityInterface;
}
8 changes: 8 additions & 0 deletions src/price/PriceWithRateInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

interface PriceWithRateInterface
{
public function getRate(): float;
}
12 changes: 12 additions & 0 deletions src/price/PriceWithSubpriceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

use hiqdev\billing\hiapi\price\SubPrices;

interface PriceWithSubpriceInterface
{
public function getSubprices(): SubPrices;

public function getSubprice(string $currencyCode);
}
8 changes: 8 additions & 0 deletions src/price/PriceWithSumsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

interface PriceWithSumsInterface
{
public function getSums(): Sums;
}
8 changes: 8 additions & 0 deletions src/price/PriceWithThresholdsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

interface PriceWithThresholdsInterface
{
public function getThresholds(): ProgressivePriceThresholdList;
}
10 changes: 10 additions & 0 deletions src/price/PriceWithUnitInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace hiqdev\php\billing\price;

use hiqdev\php\units\UnitInterface;

interface PriceWithUnitInterface
{
public function getUnit(): UnitInterface;
}
24 changes: 8 additions & 16 deletions src/price/ProgressivePrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
use Money\Money;
use Money\Parser\DecimalMoneyParser;

class ProgressivePrice extends AbstractPrice
class ProgressivePrice extends AbstractPrice implements PriceWithThresholdsInterface,
PriceWithMoneyInterface,
PriceWithQuantityInterface
{
protected ProgressivePriceThresholdList $thresholds;

protected Money $price;
use HasMoney;
use HasQuantity;

protected QuantityInterface $prepaid;
protected ProgressivePriceThresholdList $thresholds;

public function __construct(
$id,
Expand All @@ -30,7 +31,8 @@ public function __construct(
Money $price,
ProgressivePriceThresholdList $thresholds,
?PlanInterface $plan = null
) {
)
{
parent::__construct($id, $type, $target, $plan);
$this->thresholds = $thresholds;
$this->price = $price;
Expand All @@ -42,16 +44,6 @@ public function getThresholds(): ProgressivePriceThresholdList
return $this->thresholds;
}

public function getPrepaid(): QuantityInterface
{
return $this->prepaid;
}

public function getPrice(): Money
{
return $this->price;
}

/**
* @inheritDoc
*/
Expand Down
5 changes: 2 additions & 3 deletions src/price/RatePrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class RatePrice extends AbstractPrice
class RatePrice extends AbstractPrice implements PriceWithRateInterface
{
/** @var float */
protected $rate;
protected float $rate;

public function __construct(
$id,
Expand Down
26 changes: 4 additions & 22 deletions src/price/SinglePrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,13 @@
*
* @author Andrii Vasyliev <sol@hiqdev.com>
*/
class SinglePrice extends AbstractPrice
class SinglePrice extends AbstractPrice implements PriceWithQuantityInterface, PriceWithMoneyInterface
{
/**
* @var QuantityInterface prepaid quantity also implies Unit
* XXX cannot be null cause Unit is required
*/
protected $prepaid;

/**
* @var Money
*/
protected $price;
use HasMoney;
use HasQuantity;

public function __construct(
$id,
$id,
TypeInterface $type,
TargetInterface $target,
PlanInterface $plan = null,
Expand All @@ -53,16 +45,6 @@ public function __construct(
$this->price = $price;
}

public function getPrepaid()
{
return $this->prepaid;
}

public function getPrice()
{
return $this->price;
}

/**
* {@inheritdoc}
*/
Expand Down
Loading