TranslateOptions::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 15
nc 2
nop 6
crap 2
1
<?php
2
/**
3
 * This file is part of the badams\MicrosoftTranslator library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/badams/microsoft-translator
7
 * @package badams/microsoft-translator
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace badams\MicrosoftTranslator;
14
15
use badams\MicrosoftTranslator\Exceptions\ArgumentException;
16
17
/**
18
 * Class TranslateOptions
19
 * @package badams\MicrosoftTranslator
20
 */
21
class TranslateOptions
22
{
23
    /**
24
     * @const string
25
     */
26
    const XML_NAMESPACE_URI = 'http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2';
27
28
    /**
29
     * @const Html content type, HTML needs to be well-formed.
30
     */
31
    const CONTENT_TYPE_HTML = 'text/html';
32
33
    /**
34
     * @const plain text content type
35
     */
36
    const CONTENT_TYPE_PLAIN = 'text/plain';
37
38
    /**
39
     * @var string;
40
     */
41
    protected $category;
42
43
    /**
44
     * @var string
45
     */
46
    protected $contentType;
47
48
    /**
49
     * @var string
50
     */
51
    protected $reservedFlag;
52
53
    /**
54
     * @var string
55
     */
56
    protected $state;
57
58
    /**
59
     * @var string
60
     */
61
    protected $uri;
62
63
    /**
64
     * @var string
65
     */
66
    protected $user;
67
68
    /**
69
     * TranslateOptions constructor.
70
     * @param $category
71
     * @param $contentType
72
     * @param $reservedType
73
     * @param $state
74
     * @param $uri
75
     * @param $user
76
     */
77 33
    public function __construct(
78
        $category = 'general',
79
        $contentType = TranslateOptions::CONTENT_TYPE_PLAIN,
80
        $state = null,
81
        $uri = null,
82
        $user = null,
83
        $reservedFlag = null
84
    ) {
85 33
        if ($contentType !== self::CONTENT_TYPE_PLAIN) {
86 3
            throw new ArgumentException(sprintf('%s is not a valid content type.', $contentType));
87
        }
88
89 30
        $this->category = $category;
90 30
        $this->contentType = $contentType;
91 30
        $this->state = $state;
92 30
        $this->uri = $uri;
93 30
        $this->user = $user;
94 30
        $this->reservedFlag = $reservedFlag;
95 30
    }
96
97
    /**
98
     * @param string $root
99
     * @return \DOMDocument
100
     */
101 15
    public function xml($root = 'TranslateOptions')
102
    {
103 15
        $xml = new \DOMDocument();
104 15
        $options = $xml->createElementNS(self::XML_NAMESPACE_URI, $root);
105
106 15
        $options->appendChild($xml->createElement('Category', $this->category));
107 15
        $options->appendChild($xml->createElement('ContentType', $this->contentType));
108 15
        $options->appendChild($xml->createElement('ReservedFlag', $this->reservedFlag));
109 15
        $options->appendChild($xml->createElement('State', $this->state));
110 15
        $options->appendChild($xml->createElement('Uri', $this->uri));
111 15
        $options->appendChild($xml->createElement('User', $this->user));
112
113 15
        $xml->appendChild($options);
114
115 15
        return $xml;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 9
    public function __toString()
122
    {
123 9
        return $this->xml()->saveXML();
124
    }
125
}
126