Maileon PHP client  1.5.5
Easily integrate your PHP application with Maileon.
AbstractJSONWrapper.php
1 <?php
18  function toArray() {
19  $result = array();
20  $object_vars = get_object_vars($this);
21 
22  // copy each of this objects properties to an associative array
23  // indexed by the property names
24  foreach($object_vars as $key => $value) {
25  if($value == null) { continue; }
26 
27  // if $value is an object, we can call toArray on it, and it
28  // should be serialized (isEmpty returns true) than we call
29  // toArray and insert the result in our array;
30  // otherwise we insert the value as-is
31  if(gettype($value) == "object" &&
32  is_subclass_of($value, 'com_maileon_api_json_AbstractJSONWrapper')) {
33  if(!$value->isEmpty()) {
34  $result[$key] = $value->toArray();
35  }
36  } else {
37  // TODO: maybe deal with com_maileon_api_json_AbstractJSONWrapper
38  // derived classes that have 'non-serializable' properties
39  $result[$key] = $value;
40  }
41  }
42 
43  // return the resulting array
44  return $result;
45  }
46 
54  function fromArray($object_vars) {
55  // copy each key to the property named the same way; if the property
56  // is a serializable Maileon class, call fromArray on it
57  foreach($object_vars as $key => $value) {
58  if(class_exists('com_maileon_api_json_AbstractJSONWrapper') && is_subclass_of( $this->{$key},'com_maileon_api_json_AbstractJSONWrapper' )) {
59  $this->{$key}->fromArray($value);
60  } else {
61  $this->{$key} = $value;
62  }
63  }
64  }
65 
72  public function __toString() {
73  $object_vars = get_object_vars($this);
74  $elements = "";
75 
76  // add each property of this class to a string
77  foreach($object_vars as $key => $value) {
78  $elements .= $key . "=" . $value . ", ";
79  }
80 
81  return get_class($this) . " [ " . mb_substr($elements, 0, mb_strlen($elements) - 2) . " ]";
82  }
83 
89  function isEmpty() {
90  return false;
91  }
92 }