Completed
Push — master ( f5277c...2a14a8 )
by zacksleo
03:16
created

Page::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace zacksleo\yii2\cms\models;
4
5
use Yii;
6
use yii\behaviors\TimestampBehavior;
7
use yii\helpers\Url;
8
use nemmo\attachments\models\File;
9
10
/**
11
 * This is the model class for table "{{%page}}".
12
 *
13
 * @property integer $id
14
 * @property string $title
15
 * @property string $slug
16
 * @property integer $status
17
 * @property string $content
18
 * @property integer $created_at
19
 * @property integer $updated_at
20
 */
21
class Page extends \yii\db\ActiveRecord
22
{
23
    const STATUS_INACTIVE = 0;
24
    const STATUS_ACTIVE = 1;
25
26
    /**
27
     * @inheritdoc
28
     */
29 1
    public static function tableName()
30
    {
31 1
        return '{{%page}}';
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37 1
    public function rules()
38
    {
39
        return [
40 1
            [['title', 'slug'], 'required'],
41 1
            [['status', 'created_at', 'updated_at'], 'integer'],
42 1
            [['content'], 'string'],
43 1
            [['title', 'slug'], 'string', 'max' => 255],
44 1
        ];
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function attributeLabels()
51
    {
52
        return [
53
            'id' => 'ID',
54
            'title' => '标题',
55
            'slug' => '别名',
56
            'status' => '状态',
57
            'content' => '内容',
58
            'created_at' => '创建时间',
59
            'updated_at' => '更新时间',
60
        ];
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1
    public function behaviors()
67
    {
68
        return [
69
            'timestamp' => [
70 1
                'class' => TimestampBehavior::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
71 1
            ],
72
            'fileBehavior' => [
73 1
                'class' => \nemmo\attachments\behaviors\FileBehavior::className()
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
74 1
            ],
75 1
        ];
76
    }
77
78 1
    public static function getStatusList()
79
    {
80
        return [
81 1
            self::STATUS_INACTIVE => '隐藏',
82 1
            self::STATUS_ACTIVE => '显示',
83 1
        ];
84
    }
85
86
    public function getUrl()
87
    {
88
        return Url::to(['page/view', 'slug' => $this->slug]);
89
    }
90
91
    public function getImage()
92
    {
93
        if ($this->files && $this->files[0] instanceof File) {
0 ignored issues
show
Documentation introduced by
The property files does not exist on object<zacksleo\yii2\cms\models\Page>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
            return $this->files[0]->getUrl();
0 ignored issues
show
Documentation introduced by
The property files does not exist on object<zacksleo\yii2\cms\models\Page>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
        }
96
        return 'https://ws1.sinaimg.cn/large/a76d6e45gy1fj5d3ckxgej205x05vaa4.jpg';
97
    }
98
}
99