-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCleanTalkValidator.php
108 lines (96 loc) · 2.69 KB
/
CleanTalkValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* CleanTalk API CModel validator.
*
* Required set check property.
*
* @version 1.0.1
* @author CleanTalk (welcome@cleantalk.org)
* @copyright (C) 2013 Сleantalk team (https://cleantalk.org)
* @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
*/
class CleanTalkValidator extends CValidator
{
const CHECK_MESSAGE = 'message';
const CHECK_USER = 'user';
/**
* message|user
* @var string
*/
public $check;
/**
* Email attribute name in model
* @var string
*/
public $emailAttribute;
/**
* Nickname attribute name in model
* @var string
*/
public $nickNameAttribute;
/**
* CleanTalk application component ID
* @var string
*/
public $apiComponentId = 'cleanTalk';
/**
* @inheritdoc
* @var bool
*/
public $skipOnError = true;
/**
* @inheritdoc
*/
protected function validateAttribute($object, $attribute)
{
$this->checkValidateConfig($object);
/**
* @var CleanTalkApi $api
*/
$api = Yii::app()->getComponent($this->apiComponentId);
$email = $nick = '';
if ($this->emailAttribute) {
$email = $object->{$this->emailAttribute};
}
if ($this->nickNameAttribute) {
$nick = $object->{$this->nickNameAttribute};
}
if (self::CHECK_MESSAGE == $this->check) {
if (!$api->isAllowMessage($object->$attribute, $email, $nick)) {
$this->addError($object, $attribute, $this->getErrorMessage());
}
} elseif (self::CHECK_USER == $this->check) {
if (!$api->isAllowUser($email, $nick)) {
$this->addError($object, $attribute, $this->getErrorMessage());
}
}
}
/**
* Check validator configuration
* @param CModel $object
* @throws CException
* @throws DomainException
*/
protected function checkValidateConfig(CModel $object)
{
if (!Yii::app()->hasComponent($this->apiComponentId)) {
throw new CException(Yii::t(
'cleantalk',
'Application component "' . $this->apiComponentId . '" is not defined'
));
} elseif (!in_array($this->check, array(self::CHECK_MESSAGE, self::CHECK_USER))) {
throw new DomainException(Yii::t(
'cleantalk',
'Validation check property is not defined or invalid'
));
}
}
/**
* Get CleanTalk API deny message
* @return string
*/
protected function getErrorMessage()
{
return Yii::app()->getComponent($this->apiComponentId)->getValidationError();
}
}