Maileon PHP client  1.5.0
Easily integrate your PHP application with Maileon.
Contact.php
1 <?php
2 
10 {
11  public $id;
12  public $email;
13  public $permission;
14  public $external_id;
15  public $anonymous;
16  public $created;
17  public $updated;
18  public $standard_fields;
19  public $custom_fields;
20 
38  function __construct(
39  $id = null,
40  $email = null,
41  $permission = NULL,
42  $external_id = -1,
43  $anonymous = false,
44  $standard_fields = array(),
45  $custom_fields = array(),
46  $created = null,
47  $updated = null)
48  {
49  $this->id = $id;
50  $this->email = $email;
51  $this->permission = $permission;
52  $this->external_id = $external_id;
53  $this->anonymous = $anonymous;
54  $this->standard_fields = $standard_fields;
55  $this->custom_fields = $custom_fields;
56  $this->created = $created;
57  $this->updated = $updated;
58  }
59 
66  function fromXML($xmlElement)
67  {
68 
69  if (isset($xmlElement->id)) $this->id = $xmlElement->id;
70  $this->email = (string)$xmlElement->email;
71  if (isset($xmlElement->permission)) $this->permission = com_maileon_api_contacts_Permission::getPermission((string)$xmlElement->permission);
72  if (isset($xmlElement->external_id)) (string)$this->external_id = $xmlElement->external_id;
73  if (isset($xmlElement->anonymous)) (string)$this->anonymous = $xmlElement->anonymous;
74  if (isset($xmlElement['anonymous'])) $this->anonymous = $xmlElement['anonymous'];
75 
76  if (isset($xmlElement->created)) $this->created = $xmlElement->created;
77  if (isset($xmlElement->updated)) $this->updated = $xmlElement->updated;
78 
79  if (isset($xmlElement->standard_fields)) {
80  $this->standard_fields = array();
81  foreach ($xmlElement->standard_fields->children() as $field) {
82  $this->standard_fields[trim($field->name)] = (string)$field->value; // The trim is required to make a safer string from the object
83  }
84  }
85 
86  if (isset($xmlElement->custom_fields)) {
87  foreach ($xmlElement->custom_fields->children() as $field) {
88  $this->custom_fields[trim($field->name)] = (string)$field->value; // The trim is required to make a safer string from the object
89  }
90  }
91  }
92 
101  function toXML($addXMLDeclaration = true)
102  {
103  $xmlString = $addXMLDeclaration ? "<?xml version=\"1.0\"?><contact></contact>" : "<contact></contact>";
104  $xml = new SimpleXMLElement($xmlString);
105 
106  // Some fields are mandatory, especially when setting data to the API
107  if (isset($this->id)) $xml->addChild("id", $this->id);
108 
109  // As shown in http://stackoverflow.com/questions/17027043/unterminated-entity-reference-php a & char causes trouble with addChild.
110  // Use this workaround
111  if (isset($this->email)) {
112  $emailChild = $xml->addChild("email");
113  $xml->email = $this->email;
114  }
115 
116  if (isset($this->permission)) $xml->addChild("permission", $this->permission->getCode());
117  if (isset($this->external_id) && $this->external_id != -1) $xml->addChild("external_id", $this->external_id);
118  if (isset($this->anonymous)) $xml->addChild("anonymous", $this->anonymous);
119 
120  if (isset($this->created)) $xml->addChild("created", $this->created);
121  if (isset($this->updated)) $xml->addChild("updated", $this->updated);
122 
123  if (isset($this->standard_fields)) {
124  $standard_fields = $xml->addChild("standard_fields");
125  foreach ($this->standard_fields as $index => $value) {
126  $field = $standard_fields->addChild("field");
127  $field->addChild("name", $index);
128 
129  com_maileon_api_xml_XMLUtils::addChildAsCDATA($field, "value", $value);
130  //$field->addChild("value", $value);
131  }
132  }
133 
134  if (isset($this->custom_fields)) {
135  $customfields = $xml->addChild("custom_fields");
136  foreach ($this->custom_fields as $index => $value) {
137  $field = $customfields->addChild("field");
138  $field->addChild("name", $index);
139 
140  com_maileon_api_xml_XMLUtils::addChildAsCDATA($field, "value", $value);
141  //$field->addChild("value", $value);
142  }
143  }
144 
145  return $xml;
146  }
147 
154  function toXMLString()
155  {
156  $xml = $this->toXML();
157  return $xml->asXML();
158  }
159 
166  function toString()
167  {
168 
169  // Generate standard field string
170  $standard_fields = "";
171  if (isset($this->standard_fields)) {
172  foreach ($this->standard_fields as $index => $value) {
173  $standard_fields .= $index . "=" . $value . ",";
174  }
175  $standard_fields = rtrim($standard_fields, ',');
176  }
177 
178  // Generate custom field string
179  $customfields = "";
180  if (isset($this->custom_fields)) {
181  foreach ($this->custom_fields as $index => $value) {
182  $customfields .= $index . "=" . $value . ",";
183  }
184  $customfields = rtrim($customfields, ',');
185  }
186 
187  $permission = "";
188  if (isset($this->permission)) {
189  $permission = $this->permission->getCode();
190  }
191 
192  return "Contact [id=" . $this->id . ", email="
193  . $this->email . ", permission=" . $permission . ", external_id=" . $this->external_id
194  . ", anonymous=" . (($this->anonymous == true) ? "true" : "false") . ", created=" . $this->created . ", updated=" . $this->updated
195  . ", standard_fields={" . $standard_fields . "}, customfields={" . $customfields . "}]";
196  }
197 
204  function toCsvString()
205  {
206 
207  // Generate standard field string
208  $standard_fields = "{";
209  if (isset($this->standard_fields)) {
210  foreach ($this->standard_fields as $index => $value) {
211  $standard_fields .= $index . "=" . $value . ",";
212  }
213  $standard_fields = rtrim($standard_fields, ',');
214  }
215  $standard_fields .= "}";
216 
217  // Generate custom field string
218  $customfields = "{";
219  if (isset($this->custom_fields)) {
220  foreach ($this->custom_fields as $index => $value) {
221  $customfields .= $index . "=" . $value . ",";
222  }
223  $customfields = rtrim($customfields, ',');
224  }
225  $customfields .= "}";
226 
227  $permission = "";
228  if (isset($this->permission)) {
229  $permission = $this->permission->getCode();
230  }
231 
232  return $this->id
233  . ";" . $this->email
234  . ";" . $permission
235  . ";" . $this->external_id
236  . ";" . (($this->anonymous == true) ? "true" : "false")
237  . ";" . $this->created
238  . ";" . $this->updated
239  . ";\"" . $standard_fields . "\""
240  . ";\"" . $customfields . "\"";
241  }
242 }
static addChildAsCDATA($parent, $name, $value=NULL)
Definition: XMLUtils.php:30
toXML($addXMLDeclaration=true)
Definition: Contact.php:101
__construct($id=null, $email=null, $permission=NULL, $external_id=-1, $anonymous=false, $standard_fields=array(), $custom_fields=array(), $created=null, $updated=null)
Definition: Contact.php:38