/**
  * Fills object value from Array list
  *
  * @param $arr
  * @return $this
  */
 public function fromArray($arr)
 {
     if (!empty($arr)) {
         // Iterate over each element in array
         foreach ($arr as $k => $v) {
             // If the value is an array, it means, it is an object after conversion
             if (is_array($v)) {
                 // Determine the class of the object
                 if (($clazz = ReflectionUtil::getPropertyClass(get_class($this), $k)) != null) {
                     // If the value is an associative array, it means, its an object. Just make recursive call to it.
                     if (empty($v)) {
                         if (ReflectionUtil::isPropertyClassArray(get_class($this), $k)) {
                             // It means, it is an array of objects.
                             $this->assignValue($k, array());
                             continue;
                         }
                         $o = new $clazz();
                         //$arr = array();
                         $this->assignValue($k, $o);
                     } elseif (ArrayUtil::isAssocArray($v)) {
                         /** @var BlockCypherModel $o */
                         $o = new $clazz();
                         $o->fromArray($v);
                         $this->assignValue($k, $o);
                     } else {
                         // Else, value is an array of object/data
                         $arr = array();
                         // Iterate through each element in that array.
                         foreach ($v as $nk => $nv) {
                             if (is_array($nv)) {
                                 //BlockCypherLoggingManager::getInstance()->debug("new instance of class: $clazz");
                                 if (!class_exists($clazz)) {
                                     BlockCypherLoggingManager::getInstance()->error("Class not found: {$clazz}");
                                 } else {
                                     try {
                                         $o = new $clazz();
                                         $o->fromArray($nv);
                                         $arr[$nk] = $o;
                                     } catch (\Exception $e) {
                                         BlockCypherLoggingManager::getInstance()->error($e->getMessage());
                                     }
                                 }
                             } else {
                                 $arr[$nk] = $nv;
                             }
                         }
                         $this->assignValue($k, $arr);
                     }
                 } else {
                     $this->assignValue($k, $v);
                 }
             } else {
                 $this->assignValue($k, $v);
             }
         }
     }
     return $this;
 }
Exemple #2
0
 public function testIsAssocArray()
 {
     $arr = array(1, 2, 3);
     $this->assertEquals(false, ArrayUtil::isAssocArray($arr));
     $arr = array('name' => 'John Doe', 'City' => 'San Jose');
     $this->assertEquals(true, ArrayUtil::isAssocArray($arr));
     $arr[] = 'CA';
     $this->assertEquals(false, ArrayUtil::isAssocArray($arr));
 }