for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* PHP Billing Library
*
* @link https://github.com/hiqdev/php-billing
* @package php-billing
* @license BSD-3-Clause
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
*/
namespace hiqdev\php\billing\price;
use hiqdev\php\billing\plan\PlanInterface;
use hiqdev\php\billing\target\TargetInterface;
use hiqdev\php\billing\type\TypeInterface;
use hiqdev\php\units\Quantity;
use hiqdev\php\units\QuantityInterface;
use Money\Money;
* Single Price.
* - no charge for quantity less then prepaid
* - same price for any quantity above prepaid
* @see PriceInterface
* @author Andrii Vasyliev <[email protected]>
class SinglePrice extends AbstractPrice implements PriceWithQuantityInterface, PriceWithMoneyInterface
{
use HasMoney;
use HasQuantity;
public function __construct(
$id,
TypeInterface $type,
TargetInterface $target,
PlanInterface $plan = null,
QuantityInterface $prepaid,
Money $price
) {
parent::__construct($id, $type, $target, $plan);
$this->prepaid = $prepaid;
$this->price = $price;
}
* {@inheritdoc}
public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
$usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
if ($usage->isPositive()) {
return $usage;
return Quantity::create($this->prepaid->getUnit()->getName(), 0);
* Same price for any usage.
public function calculatePrice(QuantityInterface $usage): ?Money
return $this->price;