Created
November 15, 2015 12:07
-
-
Save leihog/d17111f7b6e5128aad8c to your computer and use it in GitHub Desktop.
Example of how to use reflection to extract values from and restore a MangoPaySDK OAuthToken
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
use \MangoPay\Libraries\OAuthToken; | |
function extractValues(OAuthToken $token) { | |
$ref = new \ReflectionClass(OAuthToken::CLASS); | |
$values = []; | |
$props = $ref->getProperties(); | |
foreach($props as $prop) { | |
if (!$prop->isPublic()) { | |
$prop->setAccessible(true); | |
} | |
$values[$prop->getName()] = $prop->getValue($token); | |
} | |
return $values; | |
} | |
function restoreToken(array $values) { | |
$ref = new \ReflectionClass(OAuthToken::CLASS); | |
$obj = $ref->newInstanceWithoutConstructor(); | |
$props = $ref->getProperties(); | |
foreach($props as $prop) { | |
$name = $prop->getName(); | |
if (array_key_exists($name, $values)) { | |
if (!$prop->isPublic()) { | |
$prop->setAccessible(true); | |
} | |
$prop->setValue($obj, $values[$name]); | |
} | |
} | |
return $obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment