Skip to content

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:

php
use craft\commerce\behaviors\CurrencyAttributeBehavior;

class LineItem extends Model
{
    public function behaviors()
    {
        return [
            'asCurrency' => [
                'class' => CurrencyAttributeBehavior::className(),
                'currencyAttributes' => [
                    'salePrice'
                    'subtotal'
                ],
                'defaultCurrency' => 'usd'
            ],
        ];
    }

    // ...
}

View source

Public Properties ​

PropertyDescription
attributeCurrencyMaparray – Mapping of attribute => currency if the default is not desired
currencyAttributesarray – Currency attributes For example: php [ 'salePrice' 'subtotal' ]
defaultCurrencystring
owneryii\base\Component, null – The owner of this behavior

attributeCurrencyMap ​

Type
array
Default value
[]

Mapping of attribute => currency if the default is not desired

View source

currencyAttributes ​

Type
array
Default value
null

Currency attributes For example:

php
[
 'salePrice'
 'subtotal'
]

View source

defaultCurrency ​

Type
string
Default value
null
Since
5.0.0

View source

Public Methods ​

MethodDescription
__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.

View source

Arguments ​

  • $name (string) – The method name
  • $params (array) – Method parameters

Returns ​

mixed – The method return value

Throws ​

__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

Arguments ​

  • $name (string) – The property name

Returns ​

mixed – The property value

Throws ​

__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

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);

View source

Arguments ​

  • $name (string) – The property name
  • $checkVars (boolean) – Whether to treat member variables as properties

Returns ​

boolean – Whether the property can be read

currencyFields() ​

View source

defineFields() ​

View source

Arguments ​

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:

php
[
    Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
    Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
]

View source

Returns ​

array – Events (array keys) and the corresponding event handler methods (array values).

getDefaultCurrency() ​

Since
5.0.0

View source

Returns ​

string

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

Arguments ​

  • $name (string) – The method name

Returns ​

boolean – Whether the method is defined

setDefaultCurrency() ​

Since
5.0.0

View source

Arguments ​

Returns ​

void