Created
March 13, 2012 10:29
-
-
Save armetiz/2028036 to your computer and use it in GitHub Desktop.
PHP - Object to Array
This file contains hidden or 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
public function normalize($object, $format = null) | |
{ | |
$reflectionObject = new \ReflectionObject($object); | |
$reflectionProperties = $reflectionObject->getProperties(\ReflectionMethod::IS_PUBLIC); | |
$attributes = array(); | |
foreach ($reflectionProperties as $property) { | |
$attributeName = $property->getName(); | |
$attributeValue = $property->getValue ($object); | |
if (null !== $attributeValue && !is_scalar($attributeValue)) { | |
$attributeValue = $this->serializer->normalize($attributeValue, $format); | |
} | |
$attributes[$attributeName] = $attributeValue; | |
} | |
return $attributes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment