1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hauntd\vote\behaviors; |
4
|
|
|
|
5
|
|
|
use hauntd\vote\models\VoteAggregate; |
6
|
|
|
use hauntd\vote\traits\ModuleTrait; |
7
|
|
|
use yii\base\Behavior; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class VoteBehavior |
12
|
|
|
* @package hauntd\vote\behaviors |
13
|
|
|
*/ |
14
|
|
|
class VoteBehavior extends Behavior |
15
|
|
|
{ |
16
|
|
|
use ModuleTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $voteAttributes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param \yii\base\Component $owner |
25
|
|
|
*/ |
26
|
|
|
public function attach($owner) |
27
|
|
|
{ |
28
|
|
|
parent::attach($owner); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $name |
33
|
|
|
* @return VoteAggregate|null |
34
|
|
|
* @throws \yii\base\InvalidConfigException |
35
|
|
|
*/ |
36
|
|
|
public function getVoteAggregate($name) |
37
|
|
|
{ |
38
|
|
|
$entities = $this->getModule()->entities; |
39
|
|
|
if (isset($entities[$name])) { |
40
|
|
|
return new VoteAggregate([ |
41
|
|
|
'entity' => $this->getModule()->encodeEntity($name), |
42
|
|
|
'target_id' => $this->owner->getPrimaryKey(), |
|
|
|
|
43
|
|
|
'positive' => ArrayHelper::getValue($this->voteAttributes, ["{$name}Positive"]), |
44
|
|
|
'negative' => ArrayHelper::getValue($this->voteAttributes, ["{$name}Negative"]), |
45
|
|
|
'rating' => ArrayHelper::getValue($this->voteAttributes, ["{$name}Rating"]), |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $name |
53
|
|
|
* @return null|integer |
54
|
|
|
* @throws \yii\base\InvalidConfigException |
55
|
|
|
*/ |
56
|
|
|
public function getUserValue($name) |
57
|
|
|
{ |
58
|
|
|
$entities = $this->getModule()->entities; |
59
|
|
|
if (isset($entities[$name])) { |
60
|
|
|
return ArrayHelper::getValue($this->voteAttributes, ["{$name}UserValue"]); |
61
|
|
|
} |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $name |
67
|
|
|
* @param mixed $value |
68
|
|
|
* @throws \yii\base\UnknownPropertyException |
69
|
|
|
*/ |
70
|
|
|
public function __set($name, $value) |
71
|
|
|
{ |
72
|
|
|
if ($this->checkAttribute($name)) { |
73
|
|
|
$this->voteAttributes[$name] = !is_null($value) ? (int) $value : null; |
74
|
|
|
} else { |
75
|
|
|
parent::__set($name, $value); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $name |
81
|
|
|
* @return bool |
82
|
|
|
* @throws \yii\base\InvalidConfigException |
83
|
|
|
*/ |
84
|
|
|
protected function checkAttribute($name) |
85
|
|
|
{ |
86
|
|
|
foreach (array_keys($this->getModule()->entities) as $entity) { |
87
|
|
|
if ($name == "{$entity}Positive" || $name == "{$entity}Negative" || $name == "{$entity}Rating" || |
88
|
|
|
$name == "{$entity}UserValue") { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $name |
97
|
|
|
* @param bool|true $checkVars |
98
|
|
|
* @return bool |
99
|
|
|
* @throws \yii\base\InvalidConfigException |
100
|
|
|
*/ |
101
|
|
|
public function canGetProperty($name, $checkVars = true) |
102
|
|
|
{ |
103
|
|
|
if (isset($this->voteAttributes[$name]) || $this->checkAttribute($name)) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
return parent::canGetProperty($name, $checkVars); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @param bool|true $checkVars |
112
|
|
|
* @return bool |
113
|
|
|
* @throws \yii\base\InvalidConfigException |
114
|
|
|
*/ |
115
|
|
|
public function canSetProperty($name, $checkVars = true) |
116
|
|
|
{ |
117
|
|
|
if ($this->checkAttribute($name)) { |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
return parent::canSetProperty($name, $checkVars); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.