Completed
Push — 1.1 ( a4c925...1876b7 )
by Daniel
07:54 queued 04:30
created

WildcardDomainField   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 38
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 2
A checkHostname() 0 4 1
A Type() 0 4 1
1
<?php
2
3
/**
4
 * A text field that accepts only valid domain names, but allows the wildcard (*) character
5
 */
6
class WildcardDomainField extends TextField
7
{
8
    /**
9
     * Validate this field as a valid hostname
10
     *
11
     * @param Validator $validator
12
     * @return bool
13
     */
14
    public function validate($validator)
15
    {
16
        if ($this->checkHostname($this->Value())) {
17
            return true;
18
        }
19
20
        $validator->validationError(
21
            $this->getName(),
22
            _t("DomainNameField.INVALID_DOMAIN", "Invalid domain name"),
23
            "validation"
24
        );
25
        return false;
26
    }
27
28
    /**
29
     * Check if the given hostname is valid.
30
     *
31
     * @param string $hostname
32
     * @return bool True if this hostname is valid
33
     */
34
    public function checkHostname($hostname)
35
    {
36
        return (bool)preg_match('/^([a-z0-9\*]+[\-\.])*([a-z0-9\*]+)$/', $hostname);
37
    }
38
39
    public function Type()
40
    {
41
        return 'text wildcarddomain';
42
    }
43
}
44