/** * Converts the MongoDB result to an object of class * whose parent is \Shared\Model */ protected function _convert($record) { if (!$record) { return null; } $columns = $this->getColumns(); $record = (array) $record; $class = get_class($this); $c = new $class(); foreach ($record as $key => $value) { if (!property_exists($this, "_{$key}")) { continue; } $raw = "_{$key}"; if (is_object($value)) { if (Db::isType($value, 'id')) { $c->{$raw} = $this->getMongoID($value); } else { if (Db::isType($value, 'date')) { $v = $value->toDateTime(); $v->settimezone(new \DateTimeZone('Asia/Kolkata')); $c->{$raw} = $v; } else { if (Db::isType($value, 'document')) { $c->{$raw} = Utils::toArray($value); } else { // fallback case $c->{$raw} = (object) $value; } } } } else { $c->{$raw} = $value; } } return $c; }
public static function toArray($object) { $arr = []; $obj = (array) $object; foreach ($obj as $key => $value) { if (Services\Db::isType($value, 'document')) { $arr[$key] = self::toArray($value); } else { $arr[$key] = $value; } } return $arr; }