XmlEncoder::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the Amplexor\XConnect library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/amplexor-drupal/xconnect/
7
 * @version 1.0.0
8
 * @package Amplexor.XConnect
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Amplexor\XConnect\Request\Encoder;
15
16
use Amplexor\XConnect\Request\Order;
17
18
/**
19
 * Format to generate the XML code representing the Order object.
20
 *
21
 * @package Amplexor\XConnect
22
 */
23
class XmlEncoder implements EncoderInterface
24
{
25
    /**
26
     * @inheritDoc
27
     */
28 27
    public function encode(Order $order)
29
    {
30 27
        $data = $this->extractData($order);
31 27
        $xml = $this->createXml($data);
32 27
        return $xml->saveXML($xml, LIBXML_NOEMPTYTAG);
33
    }
34
35
    /**
36
     * Extract the data as an array from the Order.
37
     *
38
     * @param Order $order
39
     *   The Order to extract the data from.
40
     *
41
     * @return array
42
     *   The extracted data in the same structure as the XML will be.
43
     */
44 27
    protected function extractData(Order $order)
45
    {
46
        $data = array(
47 27
            'ClientId' => $order->getClientId(),
48 27
            'OrderName' => $order->getOrderName(),
49 27
            'TemplateId' => $order->getTemplateId(),
50 27
            'RequestDate' => $order->getRequestDate()->format('Y-m-d\TH:i:s'),
51 27
            'RequestedDueDate' => $order->getDueDate()->format('Y-m-d'),
52 27
            'IssuedBy' => $order->getIssuedBy(),
53 27
            'ConfidentialOrder' => var_export($order->isConfidential(), true),
54 27
            'SourceLanguageIsoCode' => $order->getSourceLanguage(),
55 27
            'TargetLanguages' => $this->extractTargetLanguagesData($order),
56 27
            'ClientInstructions' => $this->extractInstructionsData($order),
57 27
            'ClientReference' => $order->getReference(),
58 27
            'ConfirmationRequested' => var_export($order->needsConfirmation(), true),
59 27
            'QuotationRequested' => var_export($order->needsQuotation(), true),
60 27
            'InputFiles' => $this->extractInputFilesData($order),
61 27
        );
62
63 27
        return $data;
64
    }
65
66
    /**
67
     * Create the structure for the target languages.
68
     *
69
     * @param Order $order
70
     *   The Order to extract the data from.
71
     *
72
     * @return array
73
     *   The structure for the target languages.
74
     */
75 27
    protected function extractTargetLanguagesData(Order $order)
76
    {
77 27
        $targetLanguages = $order->getTargetLanguages();
78
79 27
        $languages = array();
80 27
        foreach ($targetLanguages as $language) {
81 24
            $languages[] = array(
82 24
                'IsoCode' => $language,
83
            );
84 27
        }
85
86 27
        return $languages;
87
    }
88
89
    /**
90
     * Extract the instructions from the order (if any).
91
     *
92
     *
93
     */
94 27
    protected function extractInstructionsData(Order $order)
95
    {
96 27
        $instructions = $order->getInstructions();
97
98 27
        if (empty($instructions)) {
99 24
            return 'None';
100
        }
101
102 3
        return implode(PHP_EOL, $instructions);
103
    }
104
105
    /**
106
     * Create the structure for the files to translate.
107
     *
108
     * @param Order $order
109
     *   The Order to extract the data from.
110
     *
111
     * @return array
112
     *   The structure for the attached files.
113
     */
114 27
    protected function extractInputFilesData(Order $order)
115
    {
116 27
        $files = $order->getFiles();
117
118 27
        $inputFiles = array();
119 27
        foreach ($files as $file_name) {
120 3
            $inputFiles[] = array(
121
                'InputFile' => array(
122 3
                    'FileName' => $file_name,
123 3
                    'FileReference' => 'Input/' . $file_name,
124 3
                ),
125
            );
126 27
        }
127
128 27
        return $inputFiles;
129
    }
130
131
    /**
132
     * Create the order XML.
133
     *
134
     * @param array $data
135
     *   The data array to create the XML from.
136
     *
137
     * @return \DOMDocument
138
     *   The generated XML object.
139
     */
140 27
    protected function createXml(array $data)
141
    {
142 27
        $xml = new \DOMDocument('1.0', 'UTF-8');
143
144
        // Add the request root element.
145 27
        $request = $xml->createElementNs(
146 27
            'http://www.euroscript.com/escaepe/types',
147
            'tns:ClientWoRequest'
148 27
        );
149 27
        $request->setAttribute(
150 27
            'xmlns:xsi',
151
            'http://www.w3.org/2001/XMLSchema-instance'
152 27
        );
153 27
        $request->setAttribute(
154 27
            'xsi:schemaLocation',
155
            'http://www.euroscript.com/escaepe/types clientOrderRequestTypes.xsd'
156 27
        );
157 27
        $xml->appendChild($request);
158
159
        // Add the data to the request in the XML.
160 27
        $this->arrayToXml($data, $request);
161 27
        return $xml;
162
    }
163
164
    /**
165
     * Adds an array to an existing SimpleXMLElement object.
166
     *
167
     * @param array $array
168
     *   Array with values.
169
     * @param \DOMElement $element
170
     *   The Dom element to who the data should be added as children.
171
     */
172 27
    protected function arrayToXml(array $array, \DOMElement $element)
173
    {
174 27
        foreach ($array as $key => $value) {
175 27
            $element_key = (is_numeric($key))
176 27
                ? 'item' . $key
177 27
                : $key;
178
179 27
            if (!is_array($value)) {
180 27
                $child  = new \DOMElement(
181 27
                    $element_key,
182 27
                    htmlspecialchars($value)
183 27
                );
184 27
                $element->appendChild($child);
185 27
                continue;
186
            }
187
188
            // Support numeric keyed array values.
189 27
            if (is_numeric($key)) {
190 24
                $this->arrayToXml($value, $element);
191 24
                continue;
192
            }
193
194
            // Add the array data within a child element.
195 27
            $child = new \DOMElement($element_key);
196 27
            $element->appendChild($child);
197 27
            $this->arrayToXml($value, $child);
198 27
        }
199 27
    }
200
}
201