forked from bjmosk/OrbitalPHP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.php
117 lines (104 loc) · 2.53 KB
/
request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace OrbitalPHP;
use OrbitalPHP\Client as OrbitalPHP;
use OrbitalPHP\Exception as OrbitalPHPException;
/**
* Object representation of an Orbital gateway XML request.
*
* @author Brian Moskowitz <[email protected]>
* @package OrbitalPHP
*/
class Request
{
/**
* Orbital XML request type.
*
* @var string
*/
private $_type;
/**
* The XML payload that is generated from the values provided.
*
* @var string
*/
private $_xml;
/**
* Loads an \OrbitalPHP\Request object of the given type and constructs
* the XML from the values provided.
*
* @param string $type Orbital XML request type.
* @param array $values Key-value pairs for the XML elements, can be nested.
* @throws OrbitalPHPException
*/
public function __construct($type, $values)
{
if (!in_array($type, OrbitalPHP::validTypes())) {
throw new OrbitalPHPException('Invalid request type provided');
} else if (!is_array($values)) {
throw new OrbitalPHPException('Request values must be an array');
} else if (!$values) {
throw new OrbitalPHPException('No request values provided');
}
// Set the request type
$this->_type = $type;
// Compose and set full XML
$this->_setXml($values);
}
/**
* Returns the XML request type.
*
* @return string
*/
public function getType()
{
return $this->_type;
}
/**
* Returns the request XML.
*
* @return string
*/
public function getXml()
{
return $this->_xml;
}
/**
* Compose the XML to be sent to the Orbital gateway.
*
* @param array $values Key-value pairs for the XML elements, can be nested.
*/
private function _setXml($values)
{
$this->_xml =
'<?xml version="1.0" encoding="UTF-8"?>'
. '<Request>'
. "<{$this->getType()}>"
. $this->_composeRequestElements($values)
. "</{$this->getType()}>"
. '</Request>';
}
/**
* Composes the individual request elements. Called recursively for nested
* values.
*
* @param array $values Key-value pairs for the XML elements, can be nested.
* @return string The generated XML.
* @throws OrbitalPHPException
*/
private function _composeRequestElements($values)
{
$requestElements = '';
foreach ($values as $key => $value) {
if (is_string($value) || is_numeric($value)) {
$requestElements .= "<$key>$value</$key>";
} else if (is_array($value)) {
// call recursively
$requestElements .=
"<$key>{$this->_composeRequestElements($value)}</$key>";
} else {
throw new OrbitalPHPException("Invalid XML value for key $key");
}
}
return $requestElements;
}
}