Last active
August 29, 2015 14:02
-
-
Save bocharsky-bw/4d95dec6f200d58635c6 to your computer and use it in GitHub Desktop.
The method convert current object to array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Convert current object to array | |
*/ | |
public function toArray() | |
{ | |
$data = array(); | |
foreach (get_object_vars($this) as $property => $value) { | |
if (is_scalar($value) || is_array($value)) { | |
$data[$property] = $value; | |
} elseif (is_object($value)) { | |
if ($value instanceof \DateTime) { | |
$data[$property] = $value->getTimestamp(); | |
} elseif ($value instanceof Image) { | |
$data[$property] = $value ? $value->getWebPath() : null; | |
} else { | |
$method = 'toArray'; | |
if (method_exists($value, $method)) { | |
$data[$property] = $value->$method(); | |
} | |
} | |
} | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment