1 | <?php |
||
2 | |||
3 | |||
4 | namespace tuyakhov\notifications\models; |
||
5 | |||
6 | |||
7 | use tuyakhov\notifications\behaviors\ReadableBehavior; |
||
8 | use tuyakhov\notifications\messages\DatabaseMessage; |
||
9 | use yii\behaviors\TimestampBehavior; |
||
10 | use yii\db\ActiveRecord; |
||
11 | use yii\db\Expression; |
||
12 | use yii\helpers\ArrayHelper; |
||
13 | use yii\helpers\Json; |
||
14 | |||
15 | /** |
||
16 | * Database notification model |
||
17 | * @property string $level |
||
18 | * @property string $subject |
||
19 | * @property string $notifiable_type |
||
20 | * @property int $notifiable_id |
||
21 | * @property string $body |
||
22 | * @property string $data |
||
23 | * @property DatabaseMessage $message |
||
24 | * @property string $read_at |
||
25 | * @property $notifiable |
||
26 | * @method void markAsRead() |
||
27 | * @method void markAsUnread() |
||
28 | * @method bool read() |
||
29 | * @method bool unread() |
||
30 | */ |
||
31 | class Notification extends ActiveRecord |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * @inheritdoc |
||
36 | */ |
||
37 | public function rules() |
||
38 | { |
||
39 | return [ |
||
40 | [['level', 'notifiable_type', 'subject', 'body', 'data'], 'string'], |
||
41 | ['notifiable_id', 'integer'], |
||
42 | ]; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @inheritdoc |
||
47 | */ |
||
48 | public function behaviors() |
||
49 | { |
||
50 | return [ |
||
51 | [ |
||
52 | 'class' => TimestampBehavior::className(), |
||
53 | 'value' => new Expression('CURRENT_TIMESTAMP'), |
||
54 | ], |
||
55 | ReadableBehavior::className() |
||
56 | ]; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return \yii\db\ActiveQuery |
||
61 | */ |
||
62 | public function getNotifiable() |
||
63 | { |
||
64 | return $this->hasOne($this->notifiable_type, ['id' => 'notifiable_id']); |
||
65 | } |
||
66 | |||
67 | |||
68 | /** |
||
69 | * @param null $key |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
70 | * @return mixed |
||
71 | */ |
||
72 | public function data($key = null) |
||
73 | { |
||
74 | $data = Json::decode($this->data); |
||
75 | if ($key === null) { |
||
76 | return $data; |
||
77 | } |
||
78 | return ArrayHelper::getValue($data, $key); |
||
79 | } |
||
80 | |||
81 | } |