Maileon PHP client  1.2.5
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 = $xmlElement->email;
71  if (isset($xmlElement->permission)) $this->permission = com_maileon_api_contacts_Permission::getPermission($xmlElement->permission);
72  if (isset($xmlElement->external_id)) $this->external_id = $xmlElement->external_id;
73  if (isset($xmlElement->anonymous)) $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  if (isset($this->permission)) $xml->addChild("permission", $this->permission->getCode());
116  if (isset($this->external_id) && $this->external_id != -1) $xml->addChild("external_id", $this->external_id);
117  if (isset($this->anonymous)) $xml->addChild("anonymous", $this->anonymous);
118 
119  if (isset($this->created)) $xml->addChild("created", $this->created);
120  if (isset($this->updated)) $xml->addChild("updated", $this->updated);
121 
122  if (isset($this->standard_fields)) {
123  $standard_fields = $xml->addChild("standard_fields");
124  foreach ($this->standard_fields as $index => $value) {
125  $field = $standard_fields->addChild("field");
126  $field->addChild("name", $index);
127 
128  com_maileon_api_xml_XMLUtils::addChildAsCDATA($field, "value", $value);
129  //$field->addChild("value", $value);
130  }
131  }
132 
133  if (isset($this->custom_fields)) {
134  $customfields = $xml->addChild("custom_fields");
135  foreach ($this->custom_fields as $index => $value) {
136  $field = $customfields->addChild("field");
137  $field->addChild("name", $index);
138 
139  com_maileon_api_xml_XMLUtils::addChildAsCDATA($field, "value", $value);
140  //$field->addChild("value", $value);
141  }
142  }
143 
144  return $xml;
145  }
146 
153  function toXMLString()
154  {
155  $xml = $this->toXML();
156  return $xml->asXML();
157  }
158 
165  function toString()
166  {
167 
168  // Generate standard field string
169  $standard_fields = "";
170  if (isset($this->standard_fields)) {
171  foreach ($this->standard_fields as $index => $value) {
172  $standard_fields .= $index . "=" . $value . ",";
173  }
174  $standard_fields = rtrim($standard_fields, ',');
175  }
176 
177  // Generate custom field string
178  $customfields = "";
179  if (isset($this->custom_fields)) {
180  foreach ($this->custom_fields as $index => $value) {
181  $customfields .= $index . "=" . $value . ",";
182  }
183  $customfields = rtrim($customfields, ',');
184  }
185 
186  $permission = "";
187  if (isset($this->permission)) {
188  $permission = $this->permission->getCode();
189  }
190 
191  return "Contact [id=" . $this->id . ", email="
192  . $this->email . ", permission=" . $permission . ", external_id=" . $this->external_id
193  . ", anonymous=" . (($this->anonymous == true) ? "true" : "false") . ", created=" . $this->created . ", updated=" . $this->updated
194  . ", standard_fields={" . $standard_fields . "}, customfields={" . $customfields . "}]";
195  }
196 
203  function toCsvString()
204  {
205 
206  // Generate standard field string
207  $standard_fields = "{";
208  if (isset($this->standard_fields)) {
209  foreach ($this->standard_fields as $index => $value) {
210  $standard_fields .= $index . "=" . $value . ",";
211  }
212  $standard_fields = rtrim($standard_fields, ',');
213  }
214  $standard_fields .= "}";
215 
216  // Generate custom field string
217  $customfields = "{";
218  if (isset($this->custom_fields)) {
219  foreach ($this->custom_fields as $index => $value) {
220  $customfields .= $index . "=" . $value . ",";
221  }
222  $customfields = rtrim($customfields, ',');
223  }
224  $customfields .= "}";
225 
226  $permission = "";
227  if (isset($this->permission)) {
228  $permission = $this->permission->getCode();
229  }
230 
231  return $this->id
232  . ";" . $this->email
233  . ";" . $permission
234  . ";" . $this->external_id
235  . ";" . (($this->anonymous == true) ? "true" : "false")
236  . ";" . $this->created
237  . ";" . $this->updated
238  . ";\"" . $standard_fields . "\""
239  . ";\"" . $customfields . "\"";
240  }
241 }
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