Passed
Push — master ( 43888e...80ce50 )
by Christopher
02:32 queued 30s
created

xsLanguage::isOK()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 14
nc 5
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Barnso
5
 * Date: 30/06/2017
6
 * Time: 8:18 PM
7
 */
8
9
namespace AlgoWeb\xsdTypes;
10
11
/**
12
 * The type xsd:language represents a natural language identifier, generally used to indicate the language of a document or a part of a document. Before creating a new attribute of type xsd:language, consider using the xml:lang attribute that is intended to indicate the natural language of the element and its content.
13
 *
14
 * Values of the xsd:language type conform to RFC 3066, Tags for the Identification of Languages. The three most common formats are:
15
 *
16
 * For ISO-recognized languages, the format is a two- or three-letter, (usually lowercase) language code that conforms to ISO 639, optionally followed by a hyphen and a two-letter, (usually uppercase) country code that conforms to ISO 3166. For example, en or en-US.
17
 * For languages registered by the Internet Assigned Numbers Authority (IANA), the format is i-langname, where langname is the registered name. For example, i-navajo.
18
 * For unofficial languages, the format is x-langname, where langname is a name of up to eight characters agreed upon by the two parties sharing the document. For example, x-Newspeak.
19
 * Any of these three formats may have additional parts, each preceded by a hyphen, which identify additional countries or dialects. Schema processors will not verify that values of the xsd:language type conform to the above rules. They will simply validate based on the pattern specified for this type.
20
 *
21
 * @package AlgoWeb\xsdTypes
22
 */
23
class xsLanguage extends xsToken
24
{
25
    /**
26
     * Construct
27
     *
28
     * @param mixed $value
29
     */
30
    public function __construct($value)
31
    {
32
        parent::__construct($value);
33
        $this->setPatternFacet("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*");
34
        $this->setWhiteSpaceFacet("collapse");
35
    }
36
    protected function fixValue($value)
37
    {
38
        $value = trim($value);
39
        return parent::fixValue($value);
40
    }
41
    protected function isOK($value)
42
    {
43
        parent::isOK($value);
44
        if (null == $value) {
45
            throw new \InvalidArgumentException(
46
                "the provided value for " . __CLASS__ . " can not be null "
47
            );
48
        }
49
        if (!is_string($value)) {
50
            throw new \InvalidArgumentException(
51
                "the provided value for " . __CLASS__ . " Must be a string "
52
            );
53
        }
54
        if (empty(trim($value))) {
55
            throw new \InvalidArgumentException(
56
                "the provided value for " . __CLASS__ . " Must be a not be a blank string "
57
            );
58
        }
59
        if (8 < strlen($value)) {
60
            throw new \InvalidArgumentException(
61
                "the provided value for " . __CLASS__ . " Must be a not be longer then 8 characters "
62
            );
63
        }
64
    }
65
}
66