1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace P2A\YourMembership\Core; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class Request |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public static $API_VERSION = '2.25'; |
12
|
|
|
public static $BASE_URL = 'https://api.yourmembership.com'; |
13
|
|
|
|
14
|
|
|
public static $callID = 1; |
15
|
|
|
|
16
|
|
|
private $apiKey; |
17
|
|
|
private $saPasscode; |
18
|
|
|
private $version; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
function __construct(string $apiKey, string $saPasscode) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$this->apiKey = $apiKey; |
24
|
|
|
$this->saPasscode = $saPasscode; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create the Base Envelope for an API call to YourMembership |
29
|
|
|
* @method buildBasePayload |
30
|
|
|
* @author PA |
31
|
|
|
* @date 2017-01-09 |
32
|
|
|
* @return SimpleXMLElement XML Envelope with necessary credential parameters |
33
|
|
|
*/ |
34
|
|
|
public function buildBasePayload() : \SimpleXMLElement |
35
|
|
|
{ |
36
|
|
|
/* |
37
|
|
|
<YourMembership> |
38
|
|
|
<Version>2.25</Version> |
39
|
|
|
<ApiKey>3D638C5F-CCE2-4638-A2C1-355FA7BBC917</ApiKey> |
40
|
|
|
<CallID>001</CallID> |
41
|
|
|
<SaPasscode>************</SaPasscode> |
42
|
|
|
<Call Method="Sa.People.Profile.Get"> |
43
|
|
|
<ID>8D88D43A-B15B-4041-BEA0-89B05B2D9540</ID> |
44
|
|
|
</Call> |
45
|
|
|
</YourMembership> |
46
|
|
|
*/ |
47
|
|
|
|
48
|
|
|
$xml = new \SimpleXMLElement('<YourMembership></YourMembership>'); |
49
|
|
|
$xml->addChild('Version', self::$API_VERSION); |
50
|
|
|
$xml->addChild('ApiKey', $this->apiKey); |
51
|
|
|
$xml->addChild('CallID', self::$callID); |
52
|
|
|
$xml->addChild('SaPasscode', $this->saPasscode); |
53
|
|
|
|
54
|
|
|
return $xml; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generates the XML for a API method call within |
59
|
|
|
* @method createCallPayload |
60
|
|
|
* @author PA |
61
|
|
|
* @date 2017-01-09 |
62
|
|
|
* @param string $method YourMembership API Function Name |
63
|
|
|
* @param array $arguments Array of Arguments to be passed as part of the YourMembership "Call" |
64
|
|
|
* @return \SimpleXMLElement |
65
|
|
|
*/ |
66
|
|
|
public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement |
67
|
|
|
{ |
68
|
|
|
|
69
|
|
|
$call = new \SimpleXMLElement('<Call></Call>'); |
70
|
|
|
$call->addAttribute('method',$method); |
71
|
|
|
|
72
|
|
|
foreach ($arguments as $key => $value) { |
73
|
|
|
$call->addChild($key,$value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $call; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function buildXMLPayload(string $method, array $arguments) : \SimpleXMLElement |
80
|
|
|
{ |
81
|
|
|
$xml = $this->buildBasePayload(); |
82
|
|
|
$callPayload = $this->createCallPayload($method,$arguments); |
83
|
|
|
|
84
|
|
|
$this->sxmlAppend($xml,$callPayload); |
85
|
|
|
|
86
|
|
|
return $xml; |
87
|
|
|
} |
88
|
|
|
/** |
89
|
|
|
* Complete XML envelope |
90
|
|
|
* @method call |
91
|
|
|
* @author PA |
92
|
|
|
* @date 2017-01-09 |
93
|
|
|
* @param string $method [description] |
94
|
|
|
* @param array $arguments [description] |
95
|
|
|
* @return [type] [description] |
|
|
|
|
96
|
|
|
*/ |
97
|
|
|
public function call(string $method, array $arguments) { |
98
|
|
|
|
99
|
|
|
$xml = $this->buildXMLPayload($method, $arguments); |
100
|
|
|
|
101
|
|
|
self::$callId++; //Update the Call ID as they need to be unique per call |
102
|
|
|
|
103
|
|
|
return $xml; |
104
|
|
|
} |
105
|
|
|
/** |
106
|
|
|
* Deep Copy for SimpleXML |
107
|
|
|
* @method sxmlAppend |
108
|
|
|
* @author PA |
109
|
|
|
* @date 2017-01-09 |
110
|
|
|
* @param SimpleXMLElement $to |
111
|
|
|
* @param SimpleXMLElement $from |
112
|
|
|
*/ |
113
|
|
|
private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from) { |
114
|
|
|
$toDom = dom_import_simplexml($to); |
115
|
|
|
$fromDom = dom_import_simplexml($from); |
116
|
|
|
$toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
}; |
120
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.