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
|
|
|
public $rangeAttribute = 'range'; |
34
|
|
|
|
35
|
|
|
public function getRange() |
36
|
|
|
{ |
37
|
|
|
$rangeAttribute = $this->rangeAttribute; |
38
|
|
|
$range = Json::decode($this->$rangeAttribute); |
39
|
|
|
if (!isset($range['exclude']) || !$range['exclude']) { |
40
|
|
|
$range['exclude'] = false; |
41
|
|
|
} else { |
42
|
|
|
$range['exclude'] = true; |
43
|
|
|
} |
44
|
|
|
if (!isset($range['user'])) { |
45
|
|
|
$range['user'] = []; |
46
|
|
|
} |
47
|
|
|
if (!isset($range['status'])) { |
48
|
|
|
$range['status'] = []; |
49
|
|
|
} |
50
|
|
|
return $range; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setRange($range) |
54
|
|
|
{ |
55
|
|
|
if (!is_array($range)) { |
56
|
|
|
$range = []; |
57
|
|
|
} |
58
|
|
|
if (isset($range['exclude']) && $range['exclude'] == false) { |
59
|
|
|
unset($range['exclude']); |
60
|
|
|
} |
61
|
|
|
if (isset($range['user']) && empty($range['user'])) { |
62
|
|
|
unset($range['user']); |
63
|
|
|
} |
64
|
|
|
if (isset($range['status']) && empty($range['status'])) { |
65
|
|
|
unset($range['status']); |
66
|
|
|
} |
67
|
|
|
$rangeAttribute = $this->rangeAttribute; |
68
|
|
|
return $this->$rangeAttribute = Json::encode($range); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function inRange($user, $status = null) |
72
|
|
|
{ |
73
|
|
|
$range = $this->getRange(); |
74
|
|
|
if ($status === null) { |
75
|
|
|
return $range['exclude'] ? !in_array($user, $range['user']) : in_array($user, $range['user']); |
76
|
|
|
} |
77
|
|
|
return $range['exclude'] ? (!in_array($user, $range['user']) || !in_array($status, $range['status'])) : (in_array($user, $range['user']) || in_array($status, $range['status'])); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getNotificationRangeRules() |
81
|
|
|
{ |
82
|
|
|
return [ |
83
|
|
|
[$this->rangeAttribute, 'string'], |
84
|
|
|
[$this->rangeAttribute, 'validateRange'], |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function validateRange() |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
|
|
$rangeAttribute = $this->rangeAttribute; |
92
|
|
|
Json::decode($this->$rangeAttribute); |
93
|
|
|
} catch (\Exception $ex) { |
94
|
|
|
$this->addError($ex->getMessage()); |
|
|
|
|
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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
Idable
provides a methodequalsId
that 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.