Created
March 30, 2015 11:01
-
-
Save dwickstrom/031cde590a4441904f0e to your computer and use it in GitHub Desktop.
Assert state, hidden or else
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
/** | |
* @author David Wickström <[email protected]> | |
*/ | |
trait AssertState | |
{ | |
/** | |
* Access any value off any object given a property with any visibility mode | |
* | |
* @param $field | |
* @param $object | |
* @return mixed | |
*/ | |
public function getField($field, $object) | |
{ | |
if (!is_string($field)) { | |
throw new InvalidArgumentException('First argument must have string type.'); | |
} | |
if (!is_object($object)) { | |
throw new InvalidArgumentException('Second argument must have object type.'); | |
} | |
$clonedObject = clone $object; | |
$reflector = new ReflectionProperty(get_class($clonedObject), $field); | |
$reflector->setAccessible(true); | |
return $reflector->getValue($clonedObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment