PHP Class yii2tech\balance\Manager

See also: ManagerInterface
Since: 1.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\base\Component, implements yii2tech\balance\ManagerInterface
Show file Open project: yii2tech/balance Class Usage Examples

Public Properties

Property Type Description
$accountBalanceAttribute name of the account entity attribute, which should store current balance value.
$accountLinkAttribute name of the transaction entity attribute, which should be used to link transaction entity with account entity (store associated account ID).
$amountAttribute name of the transaction entity attribute, which should store amount.
$autoCreateAccount whether to automatically create requested account, if it does not yet exist.
$dateAttribute name of the transaction entity attribute, which should store date.
$dateAttributeValue value which should be used for new transaction date composition. This can be plain value, object like [[\yii\db\Expression]] or a PHP callback, which returns it. If not set PHP time() function will be used.
$extraAccountLinkAttribute name of the transaction entity attribute, which should store additional affected account ID. This attribute will be filled only at transfer() method execution and will store ID of the account transferred from or to, depending on the context. If not set, no information about the extra account context will be saved. Note: absence of this field will affect logic of some methods like Manager::revert.

Public Methods

Method Description
decrease ( $account, $amount, $data = [] )
increase ( $account, $amount, $data = [] )
revert ( $transactionId, $data = [] )
transfer ( $from, $to, $amount, $data = [] )

Protected Methods

Method Description
afterCreateTransaction ( mixed $transactionId, mixed $accountId, array $data ) : array This method is invoked after transaction has been created.
beforeCreateTransaction ( mixed $accountId, array $data ) : array This method is invoked before creating transaction.
createAccount ( array $attributes ) : mixed Creates new account with given attributes.
createTransaction ( array $attributes ) : mixed Writes transaction data into persistent storage.
fetchAccountId ( mixed $idOrFilter ) : mixed
findAccountId ( array $attributes ) : mixed | null Finds account ID matching given filter attributes.
findTransaction ( mixed $id ) : array | null Finds transaction data by ID.
getDateAttributeValue ( ) : mixed Returns actual now date value for the transaction.
incrementAccountBalance ( mixed $accountId, integer | float $amount ) Increases current account balance value.

Method Details

afterCreateTransaction() protected method

This method is invoked after transaction has been created.
protected afterCreateTransaction ( mixed $transactionId, mixed $accountId, array $data ) : array
$transactionId mixed transaction ID.
$accountId mixed account ID.
$data array transaction data.
return array adjusted transaction data.

beforeCreateTransaction() protected method

This method is invoked before creating transaction.
protected beforeCreateTransaction ( mixed $accountId, array $data ) : array
$accountId mixed account ID.
$data array transaction data.
return array adjusted transaction data.

createAccount() abstract protected method

Creates new account with given attributes.
abstract protected createAccount ( array $attributes ) : mixed
$attributes array account attributes in format: attribute => value
return mixed created account ID.

createTransaction() abstract protected method

Writes transaction data into persistent storage.
abstract protected createTransaction ( array $attributes ) : mixed
$attributes array attributes associated with transaction in format: attribute => value
return mixed new transaction ID.

decrease() public method

public decrease ( $account, $amount, $data = [] )

fetchAccountId() protected method

protected fetchAccountId ( mixed $idOrFilter ) : mixed
$idOrFilter mixed account ID or filter condition.
return mixed account ID.

findAccountId() abstract protected method

Finds account ID matching given filter attributes.
abstract protected findAccountId ( array $attributes ) : mixed | null
$attributes array filter attributes.
return mixed | null account ID, `null` - if not found.

findTransaction() abstract protected method

Finds transaction data by ID.
abstract protected findTransaction ( mixed $id ) : array | null
$id mixed transaction ID.
return array | null transaction data, `null` - if not found.

getDateAttributeValue() protected method

Returns actual now date value for the transaction.
protected getDateAttributeValue ( ) : mixed
return mixed date attribute value.

increase() public method

public increase ( $account, $amount, $data = [] )

incrementAccountBalance() abstract protected method

Increases current account balance value.
abstract protected incrementAccountBalance ( mixed $accountId, integer | float $amount )
$accountId mixed account ID.
$amount integer | float amount to be added to the current balance.

revert() public method

public revert ( $transactionId, $data = [] )

transfer() public method

public transfer ( $from, $to, $amount, $data = [] )

Property Details

$accountBalanceAttribute public property

name of the account entity attribute, which should store current balance value.
public $accountBalanceAttribute

$accountLinkAttribute public property

name of the transaction entity attribute, which should be used to link transaction entity with account entity (store associated account ID).
public $accountLinkAttribute

$amountAttribute public property

name of the transaction entity attribute, which should store amount.
public $amountAttribute

$autoCreateAccount public property

whether to automatically create requested account, if it does not yet exist.
public $autoCreateAccount

$dateAttribute public property

name of the transaction entity attribute, which should store date.
public $dateAttribute

$dateAttributeValue public property

value which should be used for new transaction date composition. This can be plain value, object like [[\yii\db\Expression]] or a PHP callback, which returns it. If not set PHP time() function will be used.
public $dateAttributeValue

$extraAccountLinkAttribute public property

name of the transaction entity attribute, which should store additional affected account ID. This attribute will be filled only at transfer() method execution and will store ID of the account transferred from or to, depending on the context. If not set, no information about the extra account context will be saved. Note: absence of this field will affect logic of some methods like Manager::revert.
public $extraAccountLinkAttribute