CurrencyAttributeBehavior
- Type
- Class
- Namespace
- craft\commerce\behaviors
- Inherits
- craft\commerce\behaviors\CurrencyAttributeBehavior » yii\base\Behavior (opens new window) » yii\base\BaseObject (opens new window)
- Implements
- yii\base\Configurable (opens new window)
CurrencyAttributeBehavior provides an ability of automatic add *AsCurrency() methods to your models for currency attributes.
You should specify exact attribute types via currencyAttributes.
For example:
use craft\commerce\behaviors\CurrencyAttributeBehavior;
class LineItem extends Model
{
public function behaviors()
{
return [
'asCurrency' => [
'class' => CurrencyAttributeBehavior::className(),
'currencyAttributes' => [
'salePrice'
'subtotal'
],
'defaultCurrency' => 'usd'
],
];
}
// ...
}
View source (opens new window)
# Public Properties
| Property | Description |
|---|---|
| attributeCurrencyMap | array (opens new window) – Mapping of attribute => currency if the default is not desired |
| currencyAttributes | array (opens new window) – Currency attributes For example: php [ 'salePrice' 'subtotal' ] |
| defaultCurrency | string (opens new window) – Default currency |
| owner (opens new window) | yii\base\Component (opens new window), null (opens new window) – The owner of this behavior |
# attributeCurrencyMap
- Type
- array (opens new window)
- Default value
[]
Mapping of attribute => currency if the default is not desired
View source (opens new window)
# currencyAttributes
- Type
- array (opens new window)
- Default value
null
Currency attributes For example:
[
'salePrice'
'subtotal'
]
View source (opens new window)
# defaultCurrency
- Type
- string (opens new window)
- Default value
null
Default currency
View source (opens new window)
# Public Methods
| Method | Description |
|---|---|
| __call() | Calls the named method which is not a class method. |
| __construct() (opens new window) | Constructor. |
| __get() | Returns the value of an object property. |
| __isset() | Checks if a property is set, i.e. defined and not null. |
| __set() (opens new window) | Sets value of an object property. |
| __unset() (opens new window) | Sets an object property to null. |
| attach() (opens new window) | Attaches the behavior object to the component. |
| canGetProperty() | Returns a value indicating whether a property can be read. |
| canSetProperty() (opens new window) | Returns a value indicating whether a property can be set. |
| className() (opens new window) | Returns the fully qualified name of this class. |
| currencyFields() | |
| detach() (opens new window) | Detaches the behavior object from the component. |
| events() (opens new window) | Declares event handlers for the owner (opens new window)'s events. |
| hasMethod() | Returns a value indicating whether a method is defined. |
| hasProperty() (opens new window) | Returns a value indicating whether a property is defined. |
| init() (opens new window) | Initializes the object. |
# __call()
Calls the named method which is not a class method.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
View source (opens new window)
Arguments
$name(string (opens new window)) – The method name$params(array (opens new window)) – Method parameters
Returns
mixed – The method return value
Throws
- yii\base\UnknownMethodException (opens new window)
when calling unknown method
# __get()
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $value = $object->property;.
View source (opens new window)
Arguments
$name(string (opens new window)) – The property name
Returns
mixed – The property value
Throws
- yii\base\UnknownPropertyException (opens new window)
if the property is not defined - yii\base\InvalidCallException (opens new window)
if the property is write-only
# __isset()
Checks if a property is set, i.e. defined and not null.
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing isset($object->property).
Note that if the property is not defined, false will be returned.
View source (opens new window)
Arguments
$name(string (opens new window)) – The property name or the event name
Returns
boolean (opens new window) – Whether the named property is set (not null).
# canGetProperty()
Returns a value indicating whether a property can be read.
A property is readable if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVarsis true);
View source (opens new window)
Arguments
$name(string (opens new window)) – The property name$checkVars(boolean (opens new window)) – Whether to treat member variables as properties
Returns
boolean (opens new window) – Whether the property can be read
# currencyFields()
View source (opens new window)
Returns
mixed
# hasMethod()
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists().
You may override this method when you implemented the php magic method __call().
View source (opens new window)
Arguments
$name(string (opens new window)) – The method name
Returns
boolean (opens new window) – Whether the method is defined