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()
defineFields()
detach() (opens new window) Detaches the behavior object from the component.
events() 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

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 (opens new window)

Arguments

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 (opens new window)

Arguments

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 $checkVars is true);

View source (opens new window)

Arguments

Returns

boolean (opens new window) – Whether the property can be read

# currencyFields()

View source (opens new window)

# defineFields()

View source (opens new window)

Arguments

Returns

void

# events()

Declares event handlers for the owner (opens new window)'s events.

Child classes may override this method to declare what PHP callbacks should be attached to the events of the owner (opens new window) component.

The callbacks will be attached to the owner (opens new window)'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',
]

View source (opens new window)

Returns

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

# 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

Returns

boolean (opens new window) – Whether the method is defined