CurrencyAttributeBehavior ​
- Type
- Class
- Namespace
- craft\commerce\behaviors
- Inherits
- craft\commerce\behaviors\CurrencyAttributeBehavior » yii\base\Behavior » yii\base\BaseObject
- Implements
- yii\base\Configurable
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'
],
];
}
// ...
}
Public Properties ​
Property | Description |
---|---|
attributeCurrencyMap | array – Mapping of attribute => currency if the default is not desired |
currencyAttributes | array – Currency attributes For example: php [ 'salePrice' 'subtotal' ] |
defaultCurrency | string |
owner | yii\base\Component, null – The owner of this behavior |
attributeCurrencyMap
​
- Type
- array
- Default value
[]
Mapping of attribute => currency if the default is not desired
currencyAttributes
​
- Type
- array
- Default value
null
Currency attributes For example:
[
'salePrice'
'subtotal'
]
defaultCurrency
​
- Type
- string
- Default value
null
- Since
- 5.0.0
Public Methods ​
Method | Description |
---|---|
__call() | Calls the named method which is not a class method. |
__construct() | Constructor. |
__get() | Returns the value of an object property. |
__isset() | Checks if a property is set, i.e. defined and not null. |
__set() | Sets value of an object property. |
__unset() | Sets an object property to null. |
attach() | Attaches the behavior object to the component. |
canGetProperty() | Returns a value indicating whether a property can be read. |
canSetProperty() | Returns a value indicating whether a property can be set. |
className() | Returns the fully qualified name of this class. |
currencyFields() | |
defineFields() | |
detach() | Detaches the behavior object from the component. |
events() | Declares event handlers for the owner's events. |
getDefaultCurrency() | |
hasMethod() | Returns a value indicating whether a method is defined. |
hasProperty() | Returns a value indicating whether a property is defined. |
init() | Initializes the object. |
setDefaultCurrency() |
__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.
Arguments ​
Returns ​
mixed
– The method return value
Throws ​
- yii\base\UnknownMethodException
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;
.
Arguments ​
$name
(string) – The property name
Returns ​
mixed
– The property value
Throws ​
- yii\base\UnknownPropertyException
if the property is not defined - yii\base\InvalidCallException
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.
Arguments ​
$name
(string) – The property name or the event name
Returns ​
boolean – 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
$checkVars
is true);
Arguments ​
$name
(string) – The property name$checkVars
(boolean) – Whether to treat member variables as properties
Returns ​
boolean – Whether the property can be read
currencyFields()
​
defineFields()
​
Arguments ​
$event
(craft\events\DefineFieldsEvent)
Returns ​
void
events()
​
Declares event handlers for the owner's events.
Child classes may override this method to declare what PHP callbacks should be attached to the events of the owner component.
The callbacks will be attached to the owner's events when the behavior is attached to the owner; and they will be detached from the events when the behavior is detached from the component.
The callbacks can be any of the following:
- method in this behavior:
'handleClick'
, equivalent to[$this, 'handleClick']
- object method:
[$object, 'handleClick']
- static method:
['Page', 'handleClick']
- anonymous function:
function ($event) { ... }
The following is an example:
[
Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]
Returns ​
array – Events (array keys) and the corresponding event handler methods (array values).
getDefaultCurrency()
​
- Since
- 5.0.0
Returns ​
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()
.
Arguments ​
$name
(string) – The method name
Returns ​
boolean – Whether the method is defined
setDefaultCurrency()
​
- Since
- 5.0.0
Arguments ​
$value
(string)
Returns ​
void