/** * Decode value from LeanCloud response. * * @param mixed $value * @return mixed */ public static function decode($value) { if (is_null($value) || is_scalar($value)) { return $value; } if (isset($value["*"]) && isset($value["*"]["read"])) { // skip ACL for now return null; } if (!isset($value["__type"])) { $out = array(); foreach ($value as $key => $val) { $out[$key] = self::decode($val); } return $out; } // Parse different data type from server. $type = $value["__type"]; if ($type == "Date") { // return time in default time zone return new \DateTime($value["iso"]); } if ($type == "Bytes") { return LeanBytes::createFromBase64Data($value["base64"]); } if ($type == "GeoPoint") { } if ($type == "File") { $file = new LeanFile($value["name"]); $file->mergeAfterFetch($value); return $file; } if ($type == "Pointer" || $type == "Object") { $obj = LeanObject::create($value["className"], $value["objectId"]); unset($value["__type"]); unset($value["className"]); if (!empty($value)) { $obj->mergeAfterFetch($value); } return $obj; } if ($type == "Relation") { return new LeanRelation(null, null, $value["className"]); } }
/** * Decode value from LeanCloud response. * * @param mixed $value Value to decode * @param string $key Field key for the value * @return mixed */ public static function decode($value, $key) { if (!is_array($value)) { return $value; } if ($key === 'ACL') { return new LeanACL($value); } if (!isset($value["__type"])) { $out = array(); foreach ($value as $k => $v) { $out[$k] = self::decode($v, $k); } return $out; } // Parse different data type from server. $type = $value["__type"]; if ($type === "Date") { // return time in default time zone return new \DateTime($value["iso"]); } if ($type === "Bytes") { return LeanBytes::createFromBase64Data($value["base64"]); } if ($type === "GeoPoint") { return new GeoPoint($value["latitude"], $value["longitude"]); } if ($type === "File") { $file = new LeanFile($value["name"]); $file->mergeAfterFetch($value); return $file; } if ($type === "Pointer" || $type === "Object") { $obj = LeanObject::create($value["className"], $value["objectId"]); unset($value["__type"]); unset($value["className"]); if (!empty($value)) { $obj->mergeAfterFetch($value); } return $obj; } if ($type === "Relation") { return new LeanRelation(null, $key, $value["className"]); } }
/** * Find and return objects by the query * * The skip or limit provided here will have higher precedence * than the ones specified by query's `skip` and `limit` methods. * It does not mutate query. * * @param int $skip (optional) Number of rows to skip * @param int $limit (optional) Max number of rows to fetch * @return array */ public function find($skip = -1, $limit = -1) { $params = $this->encode(); if ($skip >= 0) { $params["skip"] = $skip; } if ($limit >= 0) { $params["limit"] = $limit; } $resp = LeanClient::get("/classes/{$this->getClassName()}", $params); $objects = array(); foreach ($resp["results"] as $props) { $obj = LeanObject::create($this->getClassName()); $obj->mergeAfterFetch($props); $objects[] = $obj; } return $objects; }
/** * Doing a CQL query to retrive objects * * It returns an array that contains: * * * className - Class name of the query * * results - List of objects * * count - Number of objects when it's a count query * * @param string $cql CQL statement * @param array $pvalues Positional values to replace in CQL * @return array * @link https://leancloud.cn/docs/cql_guide.html */ public static function doCloudQuery($cql, $pvalues = array()) { $data = array("cql" => $cql); if (!empty($pvalues)) { $data["pvalues"] = json_encode(LeanClient::encode($pvalues)); } $resp = LeanClient::get('/cloudQuery', $data); $objects = array(); foreach ($resp["results"] as $val) { $obj = LeanObject::create($resp["className"], $val["objectId"]); $obj->mergeAfterFetch($val); $objects[] = $obj; } $resp["results"] = $objects; return $resp; }