Completed
Push — develop ( 8d652e...8fbd9e )
by Maarten
02:08
created

XmlEncoder::extractData()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.0007

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 29
ccs 22
cts 23
cp 0.9565
rs 8.8571
cc 3
eloc 21
nc 4
nop 1
crap 3.0007
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();
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' => (int) $order->isConfidential(),
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' => (int) $order->needsConfirmation(),
59 27
            'QuotationRequested' => (int) $order->needsQuotation(),
60 27
            'InputFiles' => $this->extractInputFilesData($order),
61 27
        );
62
63
        // Remove optional elements from request order.
64 27
        if (empty($data['TemplateId'])) {
65 24
            unset($data['TemplateId']);
66 24
        }
67 27
        if (empty($data['ClientInstructions'])) {
68
            unset($data['ClientInstructions']);
69 3
        }
70
71 27
        return $data;
72
    }
73
74
    /**
75
     * Create the structure for the target languages.
76
     *
77
     * @param Order $order
78
     *   The Order to extract the data from.
79
     *
80
     * @return array
81
     *   The structure for the target languages.
82
     */
83 27
    protected function extractTargetLanguagesData(Order $order)
84
    {
85 27
        $targetLanguages = $order->getTargetLanguages();
86
87 27
        $languages = array();
88 27
        foreach ($targetLanguages as $language) {
89 24
            $languages[] = array(
90 24
                'IsoCode' => $language,
91
            );
92 27
        }
93
94 27
        return $languages;
95
    }
96
97
    /**
98
     * Extract the instructions from the order (if any).
99
     *
100
     *
101
     */
102 27
    protected function extractInstructionsData(Order $order)
103
    {
104 27
        $instructions = $order->getInstructions();
105
106 27
        if (empty($instructions)) {
107 24
            return 'None';
108
        }
109
110 3
        return implode(PHP_EOL, $instructions);
111
    }
112
113
    /**
114
     * Create the structure for the files to translate.
115
     *
116
     * @param Order $order
117
     *   The Order to extract the data from.
118
     *
119
     * @return array
120
     *   The structure for the attached files.
121
     */
122 27
    protected function extractInputFilesData(Order $order)
123
    {
124 27
        $files = $order->getFiles();
125
126 27
        $inputFiles = array();
127 27
        foreach ($files as $file_name) {
128 3
            $inputFiles[] = array(
129
                'InputFile' => array(
130 3
                    'FileName' => $file_name,
131 3
                    'FileReference' => 'Input/' . $file_name,
132 3
                ),
133
            );
134 27
        }
135
136 27
        return $inputFiles;
137
    }
138
139
    /**
140
     * Create the order XML.
141
     *
142
     * @param array $data
143
     *   The data array to create the XML from.
144
     *
145
     * @return \DOMDocument
146
     *   The generated XML object.
147
     */
148 27
    protected function createXml(array $data)
149
    {
150 27
        $xml = new \DOMDocument('1.0', 'UTF-8');
151
152
        // Add the request root element.
153 27
        $request = $xml->createElementNs(
154 27
            'http://www.euroscript.com/escaepe/types',
155
            'tns:ClientWoRequest'
156 27
        );
157 27
        $request->setAttribute(
158 27
            'xmlns:xsi',
159
            'http://www.w3.org/2001/XMLSchema-instance'
160 27
        );
161 27
        $request->setAttribute(
162 27
            'xsi:schemaLocation',
163
            'http://www.euroscript.com/escaepe/types clientOrderRequestTypes.xsd'
164 27
        );
165 27
        $xml->appendChild($request);
166
167
        // Add the data to the request in the XML.
168 27
        $this->arrayToXml($data, $request);
169 27
        return $xml;
170
    }
171
172
    /**
173
     * Adds an array to an existing SimpleXMLElement object.
174
     *
175
     * @param array $array
176
     *   Array with values.
177
     * @param \DOMElement $element
178
     *   The Dom element to who the data should be added as children.
179
     */
180 27
    protected function arrayToXml(array $array, \DOMElement $element)
181
    {
182 27
        foreach ($array as $key => $value) {
183 27
            $element_key = (is_numeric($key))
184 27
                ? 'item' . $key
185 27
                : $key;
186
187 27
            if (!is_array($value)) {
188 27
                $child  = new \DOMElement(
189 27
                    $element_key,
190 27
                    htmlspecialchars($value)
191 27
                );
192 27
                $element->appendChild($child);
193 27
                continue;
194
            }
195
196
            // Support numeric keyed array values.
197 27
            if (is_numeric($key)) {
198 24
                $this->arrayToXml($value, $element);
199 24
                continue;
200
            }
201
202
            // Add the array data within a child element.
203 27
            $child = new \DOMElement($element_key);
204 27
            $element->appendChild($child);
205 27
            $this->arrayToXml($value, $child);
206 27
        }
207 27
    }
208
}
209