Last active
March 26, 2017 12:14
-
-
Save alexsasharegan/dfeaff95538826c43f94f48239e0bfd8 to your computer and use it in GitHub Desktop.
Extracts all the properties of an instance of ANetApiResponseType to an 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 | |
class ResponseReflector { | |
/** | |
* @param \net\authorize\api\contract\v1\ANetApiResponseType $responseType | |
* | |
* @return array | |
*/ | |
public static function extractANetResponsePropsToArray( \net\authorize\api\contract\v1\ANetApiResponseType $responseType ) | |
{ | |
return self::invokeAllGetters( $responseType ); | |
} | |
/** | |
* @param $object | |
* | |
* @return array | |
*/ | |
protected static function invokeAllGetters( $object ) | |
{ | |
$returnValues = []; | |
$reflectionClass = new ReflectionClass( $object ); | |
if ( $reflectionClass ) | |
{ | |
do | |
{ | |
foreach ( $reflectionClass->getMethods() as $reflectionMethod ) | |
{ | |
$methodName = $reflectionMethod->getName(); | |
$numParams = $reflectionMethod->getNumberOfRequiredParameters(); | |
if ( preg_match( "/^get.+$/", $methodName ) && $numParams === 0 ) | |
{ | |
$propName = lcfirst( str_replace( 'get', '', $methodName ) ); | |
$returnValues[ $propName ] = self::invokeSwitch( $reflectionMethod->invoke( $object ) ); | |
} | |
} | |
} while ( $reflectionClass = $reflectionClass->getParentClass() ); | |
} | |
return $returnValues; | |
} | |
/** | |
* @param $value | |
* | |
* @return array | |
*/ | |
protected static function invokeSwitch( $value ) | |
{ | |
switch ( TRUE ) | |
{ | |
case is_array( $value ): | |
$return = []; | |
foreach ( $value as $i => $val ) | |
{ | |
$return[ $i ] = is_object( $val ) || is_array( $val ) | |
? self::invokeSwitch( $val ) | |
: $val; | |
} | |
break; | |
case is_object( $value ): | |
$return = self::invokeAllGetters( $value ); | |
break; | |
default: | |
$return = $value; | |
break; | |
} | |
return $return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment