1. Go to this page and download the library: Download elegant-bro/money library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
elegant-bro / money example snippets
declare(strict_types=1);
use ElegantBro\Money\Currencies\USD;
use ElegantBro\Money\Currency;
use ElegantBro\Money\Money;
final class UserBalance implements Money
{
/**
* @var string
*/
private $userId;
/**
* @var PDO
*/
private $pdo;
public function __construct(string $userId, PDO $pdo)
{
$this->userId = $userId;
$this->pdo = $pdo;
}
public function amount(): string
{
$stmt = $this->pdo->prepare('SELECT SUM(debit - credit) FROM balances WHERE user_id = ?');
$stmt->execute([$this->userId]);
return $stmt->fetchColumn();
}
public function currency(): Currency
{
return new USD();
}
public function scale(): int
{
return 2;
}
}
declare(strict_types=1);
use ElegantBro\Money\Currencies\USD;
use ElegantBro\Money\Currency;
use ElegantBro\Money\Money;
final class TwoDollars implements Money
{
public function amount(): string
{
return '2';
}
public function currency(): Currency
{
return new USD();
}
public function scale(): int
{
return 2;
}
}
declare(strict_types=1);
use ElegantBro\Money\ArrayLot;
use ElegantBro\Money\Money;
use ElegantBro\Money\Operations\MaxOf;
use ElegantBro\Money\Operations\Multiplied;
use ElegantBro\Money\Wrapper;
final class Tax extends Wrapper
{
public function __construct(Money $origin, Money $minTax)
{
$this->is(
new MaxOf(
new ArrayLot(
Multiplied::keepScale($origin, '0.1'),
$minTax
),
$origin->scale()
)
);
}
}
// on the client side
$tax = new Tax(
new UserBalance($uuid, $pdo),
new TwoDollars()
);
declare(strict_types=1);
use Money/Money;
class MoneyHelper
{
public static function tax(Money $amount, Money $minTax): Money
{
return Money::max(
$amount->multiply('0.1'),
$minTax
);
}
// usually helpers contain dozens of static methods
}
// on the client side
$tax = MoneyHelper::tax(
$userBalance, // you should fetch user balance from the database before
Money::USD(200)
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.