This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodaValidator.php
104 lines (90 loc) · 3.28 KB
/
DecodaValidator.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
<?php
/**
* Decoda Validator Class
*
* Validate string for parsing errors
*
* @author Vadim Vorotilov <fant.geass@gmail.com>
* @license http://opensource.org/licenses/mit-license.php Licensed under The MIT License
* @link http://milesj.me/code/php/decoda
* @version 1.0
* @uses YiiDecoda
*/
class DecodaValidator extends CValidator
{
/**
* @var boolean whether the attribute value can be null or empty. Defaults to true,
* meaning that if the attribute is empty, it is considered valid.
*/
public $allowEmpty = true;
/**
* @var Decoda
* YiiDecoda component
*/
public $decoda;
/**
* @var array
* Errors types that need to be validated
* example: array(Decoda::ERROR_NESTING, Decoda::ERROR_SCOPE)
*/
public $errorTypes;
/**
* @var bool
* if true and no errors then attribute will contain parsed string
*/
public $useParsed = true;
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel $object the object being validated
* @param string $attribute the attribute being validated
*/
protected function validateAttribute($object, $attribute)
{
$value = $object->$attribute;
if($this->allowEmpty && $this->isEmpty($value)) {
return;
}
if ($this->decoda == null) {
$this->decoda =& Yii::app()->decoda;
}
if ($this->errorTypes === null || in_array(Decoda::ERROR_ALL, $this->errorTypes)) {
$this->errorTypes = array(Decoda::ERROR_CLOSING, Decoda::ERROR_NESTING, Decoda::ERROR_SCOPE);
}
$parsedValue = $this->decoda->parse($value);
$decodaErrors = $this->decoda->getErrors();
foreach ($decodaErrors as $error) {
switch ($error['type']) {
case Decoda::ERROR_NESTING:
$nesting[] = $error['tag'];
break;
case Decoda::ERROR_CLOSING:
$closing[] = $error['tag'];
break;
case Decoda::ERROR_SCOPE:
$scope[] = $error['child'] . ' in ' . $error['parent'];
break;
}
}
$errors = array();
if (in_array(Decoda::ERROR_NESTING, $this->errorTypes) && !empty($nesting)) {
$errors[] = Yii::t('decoda', 'The following tags have been nested in the wrong order: {tags}',
array('{tags}' => implode(', ', $nesting)));
}
if (in_array(Decoda::ERROR_CLOSING, $this->errorTypes) && !empty($closing)) {
$errors[] = Yii::t('decoda', 'The following tags have no closing tag: {tags}',
array('{tags}' => implode(', ', $closing)));
}
if (in_array(Decoda::ERROR_SCOPE, $this->errorTypes) && !empty($scope)) {
$errors[] = Yii::t('decoda', 'The following tags can not be placed within a specific tag: {tags}',
array('{tags}' => implode(', ', $scope)));
}
if (!empty($errors)) {
foreach ($errors as $message) {
$this->addError($object, $attribute, $message);
}
} elseif ($this->useParsed) {
$object->$attribute = $parsedValue;
}
}
}