Exemplo n.º 1
0
 /**
  * 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(Client::encode($pvalues));
     }
     $resp = Client::get('/cloudQuery', $data);
     $objects = array();
     foreach ($resp["results"] as $val) {
         $obj = Object::create($resp["className"], $val["objectId"]);
         $obj->mergeAfterFetch($val);
         $objects[] = $obj;
     }
     $resp["results"] = $objects;
     return $resp;
 }
Exemplo n.º 2
0
 public function testCreateObjectWithId()
 {
     $obj = new Object("TestObject");
     $obj->set("foo", "bar");
     $obj->save();
     $this->assertNotEmpty($obj->getCreatedAt());
     $obj2 = Object::create("TestObject", $obj->getObjectId());
     $obj2->fetch();
     $this->assertEquals("bar", $obj2->get("foo"));
     $obj2->destroy();
 }
Exemplo n.º 3
0
 /**
  * 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 ACL($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 Bytes::createFromBase64Data($value["base64"]);
     }
     if ($type === "GeoPoint") {
         return new GeoPoint($value["latitude"], $value["longitude"]);
     }
     if ($type === "File") {
         $file = new File($value["name"]);
         $file->mergeAfterFetch($value);
         return $file;
     }
     if ($type === "Pointer" || $type === "Object") {
         $obj = Object::create($value["className"], $value["objectId"]);
         unset($value["__type"]);
         unset($value["className"]);
         if (!empty($value)) {
             $obj->mergeAfterFetch($value);
         }
         return $obj;
     }
     if ($type === "Relation") {
         return new Relation(null, $key, $value["className"]);
     }
 }