Completed
Push — master ( 74726b...b9dd81 )
by Dmitry
03:21
created

IdnValidator::convertIdnToAscii()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace hipanel\validators;
4
5
use yii\validators\PunycodeAsset;
6
7
class IdnValidator extends DomainValidator
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function validateAttribute($model, $attribute)
13
    {
14
        $model->$attribute = idn_to_ascii($model->$attribute);
15
16
        parent::validateAttribute($model, $attribute);
17
    }
18
19
    /**
20
     * @param string $value the IDN domain name that should be converted to ASCII
21
     * @return string
22
     */
23
    public function convertIdnToAscii($value)
24
    {
25
        return idn_to_ascii($value);
26
    }
27
28
    public function convertAsciiToIdn($value)
29
    {
30
        return idn_to_utf8($value);
31
    }
32
33
34
    public function clientValidateAttribute($model, $attribute, $view)
35
    {
36
        PunycodeAsset::register($view);
37
        $js = parent::clientValidateAttribute($model, $attribute, $view);
38
39
        return "value = punycode.toASCII(value); $js";
40
    }
41
}
42