Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //original code: http://stackoverflow.com/questions/1869091/convert-array-to-object-php
- static function arrayToObject($array, $class = 'stdClass', $strict = false) {
- if (!is_array($array)) {
- return $array;
- }
- /* Fix-me: testar parametros do metodo construtor
- * $optional = false;
- //$object = new $class();
- $r = new ReflectionMethod($class, '__construct');
- $params = $r->getParameters();
- foreach ($params as $param) {
- //$param is an instance of ReflectionParameter
- if(!$param->isOptional()){
- }
- }*/
- //create an instance of an class without calling class's constructor
- $object = unserialize(
- sprintf(
- 'O:%d:"%s":0:{}', strlen($class), $class
- )
- );
- if (is_array($array) && count($array) > 0) {
- foreach ($array as $name => $value) {
- $name = strtolower(trim($name));
- if (!empty($name)) {
- if(method_exists($object, 'set'.$name)){
- $object->{'set'.$name}(Util::arrayToObject($value));
- }else{
- if(($strict)){
- if(property_exists($class, $name)){
- $object->$name = Util::arrayToObject($value);
- }
- }else{
- $object->$name = Util::arrayToObject($value);
- }
- }
- }
- }
- return $object;
- } else {
- return FALSE;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement