Operators
<?php
$operator = new MyOwnMoneyOperator();
$newMoney = $money->with($operator);<?php
use SonsOfPHP\Component\Money\Operator\Money\AddMoneyOperator;
use SonsOfPHP\Component\Money\Money;
$accountBalance = Money::USD(1000);
$depositAmount = Money::USD(2000);
// The construct takes the deposit amount we will apply to the balance
$operator = new AddMoneyOperator($depositAmount);
// The operator will apply (the deposit amount) to the account balance
// and the returned MoneyInterface will be a new object
$newBalance = $operator->apply($accountBalance);
$newBalance->isEqualTo($accountBalance); // will return false
// Another way to use the Operator is
$anotherNewBalance = $accountBalance->with($operator);
$newBalance->isEqualTo($anotherNewBalance); // will return true
// Even though $newBalance and $anotherNewBalance are different objects, they
// are both USD and both have the same amount (3000)Last updated