|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.name/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 vistart |
|
10
|
|
|
* @license https://vistart.name/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace vistart\Models\traits; |
|
14
|
|
|
|
|
15
|
|
|
use yii\helpers\Json; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Description of NotificationRangeTrait |
|
19
|
|
|
* |
|
20
|
|
|
* @property array range |
|
21
|
|
|
* [ |
|
22
|
|
|
* 'user' => [ |
|
23
|
|
|
* ], |
|
24
|
|
|
* 'status' => [ |
|
25
|
|
|
* ], |
|
26
|
|
|
* 'exclude' => false, (default) |
|
27
|
|
|
* ] |
|
28
|
|
|
* @author vistart <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
trait NotificationRangeTrait |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string range attribute name. |
|
35
|
|
|
* We do not recommend you access this attribute directly. |
|
36
|
|
|
*/ |
|
37
|
|
|
public $rangeAttribute = 'range'; |
|
38
|
|
|
|
|
39
|
|
|
public function getRange() |
|
40
|
|
|
{ |
|
41
|
|
|
$rangeAttribute = $this->rangeAttribute; |
|
42
|
|
|
if (!is_string($rangeAttribute)) { |
|
43
|
|
|
return null; |
|
44
|
|
|
} |
|
45
|
|
|
try { |
|
46
|
|
|
$range = Json::decode($this->$rangeAttribute); |
|
47
|
|
|
} catch (\Exception $ex) { |
|
48
|
|
|
$range = []; |
|
49
|
|
|
} |
|
50
|
|
|
if (!isset($range['exclude']) || !$range['exclude']) { |
|
51
|
|
|
$range['exclude'] = false; |
|
52
|
|
|
} else { |
|
53
|
|
|
$range['exclude'] = true; |
|
54
|
|
|
} |
|
55
|
|
|
if (!isset($range['user'])) { |
|
56
|
|
|
$range['user'] = []; |
|
57
|
|
|
} |
|
58
|
|
|
if (!isset($range['status'])) { |
|
59
|
|
|
$range['status'] = []; |
|
60
|
|
|
} |
|
61
|
|
|
return $range; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function setRange($range) |
|
65
|
|
|
{ |
|
66
|
|
|
$rangeAttribute = $this->rangeAttribute; |
|
67
|
|
|
if (!is_string($rangeAttribute)) { |
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
if (!is_array($range)) { |
|
71
|
|
|
$range = []; |
|
72
|
|
|
} |
|
73
|
|
|
if (isset($range['exclude']) && $range['exclude'] == false) { |
|
74
|
|
|
unset($range['exclude']); |
|
75
|
|
|
} |
|
76
|
|
|
if (isset($range['user']) && empty($range['user'])) { |
|
77
|
|
|
unset($range['user']); |
|
78
|
|
|
} |
|
79
|
|
|
if (isset($range['status']) && empty($range['status'])) { |
|
80
|
|
|
unset($range['status']); |
|
81
|
|
|
} |
|
82
|
|
|
return $this->$rangeAttribute = Json::encode($range); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function inRange($user, $status = null) |
|
86
|
|
|
{ |
|
87
|
|
|
$rangeAttribute = $this->rangeAttribute; |
|
88
|
|
|
if (!is_string($rangeAttribute)) { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
$range = $this->getRange(); |
|
92
|
|
|
if ($status === null) { |
|
93
|
|
|
return $range['exclude'] ? !in_array($user, $range['user']) : in_array($user, $range['user']); |
|
94
|
|
|
} |
|
95
|
|
|
return $range['exclude'] ? (!in_array($user, $range['user']) || !in_array($status, $range['status'])) : (in_array($user, $range['user']) || in_array($status, $range['status'])); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
public function getNotificationRangeRules() |
|
99
|
|
|
{ |
|
100
|
4 |
|
return is_string($this->rangeAttribute) ? [ |
|
101
|
4 |
|
[$this->rangeAttribute, 'string'], |
|
102
|
4 |
|
[$this->rangeAttribute, 'default', 'value' => '[]'], |
|
103
|
4 |
|
[$this->rangeAttribute, 'validateRange'], |
|
104
|
4 |
|
] : []; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
public function validateRange() |
|
108
|
|
|
{ |
|
109
|
|
|
try { |
|
110
|
4 |
|
$rangeAttribute = $this->rangeAttribute; |
|
111
|
4 |
|
Json::decode($this->$rangeAttribute); |
|
112
|
4 |
|
} catch (\Exception $ex) { |
|
113
|
|
|
$this->addError($ex->getMessage()); |
|
|
|
|
|
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
4 |
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.