EnvAttributeParserBehavior
- Type
- Class
- Namespace
- craft\behaviors
- Inherits
- craft\behaviors\EnvAttributeParserBehavior » yii\base\Behavior (opens new window) » yii\base\BaseObject (opens new window)
- Implements
- yii\base\Configurable (opens new window)
- Since
- 3.1.0
EnvAttributeParserBehavior can be applied to models with attributes that can be
set to either environment variables ($VARIABLE_NAME
) or aliases (@aliasName
).
public function behaviors(): array
{
return [
'parser' => [
'class' => EnvAttributeParserBehavior::class,
'attributes' => ['attr1', 'attr2', '...'],
],
];
}
# Public Properties
Property | Description |
---|---|
attributes | string (opens new window)[], callable (opens new window)[] – The attributes names that can be set to environment variables ($VARIABLE_NAME ) and/or aliases (@aliasName ). |
owner | yii\base\Model (opens new window) |
# attributes
- Type
- string (opens new window)[], callable (opens new window)[]
- Default value
[]
The attributes names that can be set to environment
variables ($VARIABLE_NAME
) and/or aliases (@aliasName
).
If the raw (unparsed) attribute value can’t be obtained from the attribute directly ($model->foo
),
then the attribute name should be specified as an array key instead, and the value should be set to the
raw value, or a callable that returns the raw value. For example:
'attributes' => [
'foo' => '$FOO',
'bar' => function() {
return $this->_bar;
},
],
View source (opens new window)
# owner
- Type
- yii\base\Model (opens new window)
- Default value
null
View source (opens new window)
# Public Methods
Method | Description |
---|---|
__call() (opens new window) | Calls the named method which is not a class method. |
__construct() (opens new window) | Constructor. |
__get() (opens new window) | Returns the value of an object property. |
__isset() (opens new window) | 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. |
afterValidate() | Restores the original attribute values after validation occurs. |
attach() (opens new window) | Attaches the behavior object to the component. |
beforeValidate() | Replaces attribute values before validation occurs. |
canGetProperty() (opens new window) | 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. |
detach() (opens new window) | Detaches the behavior object from the component. |
events() | Declares event handlers for the owner's events. |
getUnparsedAttribute() | Returns the original value of an attribute, or null if it wasn’t set to an environment variable or alias. |
hasMethod() (opens new window) | 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. |
# afterValidate()
Restores the original attribute values after validation occurs.
View source (opens new window)
# beforeValidate()
Replaces attribute values before validation occurs.
View source (opens new window)
# 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',
]
View source (opens new window)
Returns
array (opens new window) – Events (array keys) and the corresponding event handler methods (array values).
# getUnparsedAttribute()
Returns the original value of an attribute, or null
if it wasn’t set to an environment variable or alias.
View source (opens new window)
Arguments
$attribute
(string (opens new window))