Completed
Push — master ( e30c80...53d921 )
by Loban
40:21
created

SettingsForm   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 75
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A save() 0 8 2
A rules() 0 13 1
A attributeLabels() 0 8 1
1
<?php
2
3
namespace example\models;
4
5
use yii\base\Model;
6
use yii\di\Instance;
7
use lav45\settings\Settings;
8
9
class SettingsForm extends Model
10
{
11
    /**
12
     * Settings key
13
     * @var string
14
     */
15
    public $settingsKey = 'main.settings';
16
    /**
17
     * @var Settings|string|array
18
     */
19
    public $settings = 'settings';
20
    /**
21
     * @var string
22
     */
23
    public $title;
24
    /**
25
     * @var string
26
     */
27
    public $meta_keywords;
28
    /**
29
     * @var string
30
     */
31
    public $meta_description;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function init()
37
    {
38
        parent::init();
39
        $this->settings = Instance::ensure($this->settings, Settings::className());
40
        $this->setAttributes($this->settings->get($this->settingsKey));
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    public function save()
47
    {
48
        if ($this->validate() === true) {
49
            $attributes = $this->getAttributes($this->safeAttributes());
50
            return $this->settings->set($this->settingsKey, $attributes);
51
        }
52
        return false;
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function rules()
59
    {
60
        return [
61
            [['title'], 'trim'],
62
            [['title'], 'string', 'max' => 128],
63
64
            [['meta_keywords'], 'trim'],
65
            [['meta_keywords'], 'string', 'max' => 128],
66
67
            [['meta_description'], 'trim'],
68
            [['meta_description'], 'string', 'max' => 256],
69
        ];
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function attributeLabels()
76
    {
77
        return [
78
            'title' => 'Title',
79
            'meta_keywords' => 'Meta keywords',
80
            'meta_description' => 'Meta description',
81
        ];
82
    }
83
}