forked from hglattergotz/ExactTarget-PHP-SOAP-API
/
AbstractETSubscriber.class.php
297 lines (255 loc) · 8.93 KB
/
AbstractETSubscriber.class.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
abstract class AbstractETSubscriber
{
protected $propertyNames = array(
'Addresses', // SubscriberAddress[] Indicates addresses belonging to a subscriber.
// 'Attributes', // Attribute[] Specifies attributes associated with an object.
'Client', // ClientID Specifies the account ownership and context of an object.
'CorrelationID', // xsd:string Identifies correlation of objects across several requests.
'CreatedDate', // xsd:dateTime Read-only date and time of the object's creation.
'CustomerKey', // xsd:string User-supplied unique identifier for an object within an object type.
'EmailAddress', // xsd:string Contains the email address for a subscriber. Indicates the data extension field contains email address data.
'EmailTypePreference', // EmailType The format in which email should be sent
'GlobalUnsubscribeCategory', // GlobalUnsubscribeCategory Indicates how the application handles a globally unsubscribed subscriber.
'ID', // xsd:int Read-only legacy identifier for an object. Not supported on all objects.
'Lists', // SubscriberList[] Defines lists a subscriber resides on.
'Locale', // Locale Contains the locale information for an Account.
'ModifiedDate', // Nullable`1 Last time object information was modified.
'ObjectID', // xsd:string System-controlled, read-only text string identifier for object.
'ObjectState', // xsd:string Reserved for future use.
'Owner', // Owner Describes account ownership of subscriber in an on-your-behalf account.
'PartnerKey', // xsd:string Unique identifier provided by partner for an object, accessible only via API.
'PartnerProperties', // APIProperty[] A collection of metadata supplied by client and stored by system - only accessible via API.
'PartnerType', // xsd:string Defines partner associated with a subscriber.
'PrimaryEmailAddress', // EmailAddress Indicates primary email address for a subscriber.
'PrimarySMSAddress', // SMSAddress Indicates primary SMS address for a subscriber.
'PrimarySMSPublicationStatus', // SubscriberAddressStatus Indicates the subscriber's modality status.
'Status', // SubscriberStatus Defines status of object. Status of an address.
'SubscriberKey', // xsd:string Identification of a specific subscriber.
'SubscriberTypeDefinition', // SubscriberTypeDefinition Specifies if a subscriber resides in an integration, such as Salesforce or Microsoft Dynamics CRM
'UnsubscribedDate'
);
protected $properties = array();
protected $requiredProperties = array();
protected $requiredUpdateProperties = array();
protected $modifiedProperties = array();
protected $attributeNames = array();
protected $attributes = array();
protected $requiredAttributes = array();
protected $modifiedAttributes = array();
protected $soapClient;
public function __construct()
{
$this->configure();
foreach ($this->propertyNames as $pName)
{
$this->properties[$pName] = null;
}
foreach ($this->attributeNames as $aName)
{
$this->attributes[$aName] = null;
}
$requiredProperties = array('EmailAddress');
$this->requiredProperties = array_unique(array_merge($requiredProperties, $this->requiredProperties));
$this->soapClient = ETCore::getClient();
}
/**
* This method must be implemented by the derived class handles the custom
* attribute configuration.
*/
abstract protected function configure();
public function fromArray($data)
{
foreach ($data as $k => $v)
{
if ($v === 'Attributes')
{
foreach ($v as $ak => $av)
{
$this->_setAttribute($ak, $av);
}
}
else
{
$this->_setProperty($k, $v);
}
}
}
/**
* Populate the subscriber object with the result returned from a SOAP result.
*
* @param type $soap
*/
public function fromSoapResult($soap)
{
$res = (array)$soap;
foreach ($res as $k => $v)
{
if ($k === 'Attributes')
{
if (!is_array($v))
{
$v = array($v);
}
foreach ($v as $attribute)
{
$this->_setAttribute($attribute->Name, $attribute->Value);
}
}
else
{
$this->_setProperty($k, $v);
}
}
}
public function toArray()
{
$array = $this->properties;
$array['Attributes'] = $this->attributes;
return $array;
}
/**
* Handles both insert and update depending on the need. Use this method to
* avoid larget numbers of errors from the soap api if records do not yet
* exist.
*
* Required properties: EmailAddress (and SubscriberKey, if that feature is
* enabled for your account)
*
* @return boolean
* @throws Exception
*/
public function save()
{
$this->checkRequiredProperties($this->requiredProperties);
$this->checkRequiredAttributes();
$objects = array($this->makeSoapVar());
$result = ETCore::upsert($objects);
ETCore::evaluateSoapResult($result);
$this->clearModified();
return true;
}
/**
* An explicit update. The reason for this is that for an update the number
* of required properties is different that of an insert.
*
* Required properties: EmailAddress, SubscriberKey, or ID.
*
* @return boolean
* @throws Exception
*/
public function update()
{
$this->checkRequiredProperties($this->requiredUpdateProperties);
$this->checkRequiredAttributes();
$objects = array($this->makeSoapVar());
$result = ETCore::update($objects);
ETCore::evaluateSoapResult($result);
$this->clearModified();
return true;
}
/**
* Delete the record from the list.
*/
public function delete()
{
$this->checkRequiredProperties($this->requiredProperties);
$obj = $this->makeSoapVar();
$request = new ExactTarget_DeleteRequest();
$request->Options = null;
$request->Objects = array($obj);
$result = $this->soapClient->Delete($request);
ETCore::evaluateSoapResult($result);
return true;
}
public function setProperty($name, $value)
{
return $this->_setProperty($name, $value);
}
public function setAttribute($name, $value)
{
return $this->_setAttribute($name, $value);
}
public function toSoapVarForSave()
{
return $this->makeSoapVar();
}
protected function _setProperty($name, $value)
{
if (!array_key_exists($name, $this->properties))
{
throw new ETException('Invalid property '.$name.'!');
}
if ($name === 'Attributes')
{
foreach ($value as $n => $v)
{
$this->_setAttribute($n, $v);
}
}
else
{
$this->modifiedProperties[] = $name;
}
$this->properties[$name] = $value;
return true;
}
protected function _setAttribute($name, $value)
{
if (!array_key_exists($name, $this->attributes))
{
throw new ETException('Invalid attribute '.$name.'!');
}
$this->attributes[$name] = $value;
$this->modifiedAttributes[] = $name;
return true;
}
protected function checkRequiredAttributes()
{
foreach ($this->requiredAttributes as $required)
{
if ($this->attributes[$required] === null || $this->attributes[$required] === '')
{
throw new ETException('Required attribute '.$required.' as not been set.');
}
}
return true;
}
protected function checkRequiredProperties($requiredProps)
{
foreach ($requiredProps as $required)
{
if ($this->properties[$required] === null || $this->properties[$required] === '')
{
throw new ETException('Required property '.$required.' as not been set.');
}
}
return true;
}
/**
* Helper method that creates a SoapVar object.
*
* @return SoapVar
*/
protected function makeSoapVar()
{
$subscriber = new ExactTarget_Subscriber();
$this->modifiedProperties = array_unique(array_merge($this->modifiedProperties, $this->requiredProperties));
foreach ($this->modifiedProperties as $propName)
{
$subscriber->$propName = $this->properties[$propName];
}
$subscriber->Attributes = array();
$this->modifiedAttributes = array_unique(array_merge($this->modifiedAttributes, $this->requiredAttributes));
foreach ($this->modifiedAttributes as $attrName)
{
$subscriber->Attributes[] = ETCore::newAttribute($attrName, $this->attributes[$attrName]);
}
return ETCore::toSoapVar($subscriber, 'Subscriber');
}
protected function clearModified()
{
$this->modifiedAttributes = array();
$this->modifiedProperties = array();
}
}