Completed
Push — master ( d6a123...ee0952 )
by Basil
03:01
created

DynamicModel::attributeHints()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace luya\base;
4
5
use Yii;
6
7
/**
8
 * DynamicModel extends from yii\base\Dynamic Model.
9
 *
10
 * Additional Dynamic Model to provide attribute labels and attribute hints.
11
 * 
12
 * ```php
13
 * $model = new DynamicModel(['query']);
14
 * $model->attributeLabels = ['query' => 'Search term'];
15
 * $model->attributeHints = ['query' => 'Enter a search term in order to find articles.'];
16
 * $model->addRule(['query'], 'string');
17
 * $model->addRule(['query'], 'required'); 
18
 * ```
19
 *
20
 * @author Basil Suter <[email protected]>
21
 * @since 1.0.0
22
 */
23
class DynamicModel extends \yii\base\DynamicModel
24
{
25
    /**
26
     * @var array An array with key value pairing where key is the attribute and value the label.
27
     * @since 1.0.15
28
     */
29
    public $attributeHints = [];
30
31
    /**
32
     * @var array Assignable attributes by array where key is the label key value the label for the key.
33
     */
34
    public $attributeLabels = [];
35
    
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function attributeLabels()
40
    {
41
        $labels = [];
42
        foreach ($this->attributeLabels as $key => $value) {
43
            $labels[$key] = Yii::t('app', $value);
44
        }
45
        
46
        return $labels;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function attributeHints()
53
    {
54
        $hints = [];
55
        foreach ($this->attributeHints as $key => $value) {
56
            $hints[$key] = Yii::t('app', $value);
57
        }
58
        
59
        return $hints;
60
    }
61
}
62