Example #1
0
 public function testRelationClassEncode()
 {
     $obj = new Object("TestObject");
     $rel = $obj->getRelation("likes");
     $out = $rel->encode();
     $this->assertEquals("Relation", $out["__type"]);
     $child1 = new Object("User", "abc101");
     $rel->add($child1);
     $out = $rel->encode();
     $this->assertEquals("User", $out["className"]);
 }
Example #2
0
 public function testDoCloudQueryWithPvalues()
 {
     $obj = new Object("TestObject");
     $obj->set("name", "alice");
     $obj->save();
     $resp = Query::doCloudQuery("SELECT * FROM TestObject " . "WHERE name = ? LIMIT ?", array("alice", 1));
     $this->assertGreaterThan(0, count($resp["results"]));
     $obj->destroy();
 }
Example #3
0
 public function testGeoPointLocation()
 {
     $point = new GeoPoint(25.269876, 110.333061);
     $location = new Object("Location");
     $location->set("location", $point);
     $location->save();
     $location->destroy();
 }
Example #4
0
 public function testMergeWithRelationOperation()
 {
     $child1 = new Object("TestObject", "abc101");
     $op = new RelationOperation("foo", array($child1), null);
     $child2 = new Object("TestObject", "abc102");
     $op2 = new RelationOperation("foo", null, array($child2));
     $op3 = $op->mergeWith($op2);
     $this->assertTrue($op3 instanceof RelationOperation);
     $out = $op3->encode();
     // it adds child1, removes child2
     $this->assertEquals("Batch", $out["__op"]);
     $this->assertEquals(array($child1->getPointer()), $out["ops"][0]["objects"]);
     $this->assertEquals(array($child2->getPointer()), $out["ops"][1]["objects"]);
 }
Example #5
0
 public function testSaveObjectWithFile()
 {
     $obj = new Object("TestObject");
     $obj->set("name", "alice");
     $file = File::createWithData("test.txt", "你好,中国!");
     $obj->addIn("files", $file);
     $obj->save();
     $this->assertNotEmpty($obj->getObjectId());
     $this->assertNotEmpty($file->getObjectId());
     $this->assertNotEmpty($file->getUrl());
     $file->destroy();
     $obj->destroy();
 }
Example #6
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;
 }
Example #7
0
 /**
  * Query on the parent class where child is in the relation
  *
  * @param Object $child  Child object
  * @return Query
  */
 public function getReverseQuery(Object $child)
 {
     $query = new Query($this->parent->getClassName());
     $query->equalTo($this->key, $child->getPointer());
     return $query;
 }
Example #8
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"]);
     }
 }
Example #9
0
 public function testEncodeCircularObjectAsPointer()
 {
     $a = new Object("TestObject", "id001");
     $b = new Object("TestObject", "id002");
     $c = new Object("TestObject", "id003");
     $a->set("name", "A");
     $b->set("name", "B");
     $c->set("name", "C");
     $a->addIn("likes", $b);
     $b->addIn("likes", $c);
     $c->addIn("likes", $a);
     $jsonA = Client::encode($a, "toFullJSON");
     $jsonB = $jsonA["likes"][0];
     $jsonC = $jsonB["likes"][0];
     $this->assertEquals("Object", $jsonA["__type"]);
     $this->assertEquals("Object", $jsonB["__type"]);
     $this->assertEquals("Object", $jsonC["__type"]);
     $this->assertEquals("Pointer", $jsonC["likes"][0]["__type"]);
 }
Example #10
0
 /**
  * Link user with 3rd party provider
  *
  * @param string $provider  Provider name e.g. "weibo", "weixin"
  * @param array  $authToken Array of id, token, and expiration info
  * @return self
  */
 public function linkWith($provider, $authToken)
 {
     if (!is_string($provider) || empty($provider)) {
         throw new \InvalidArgumentException("Provider name can only " . "be string.");
     }
     $data = $this->get("authData");
     if (!$data) {
         $data = array();
     }
     $data[$provider] = $authToken;
     $this->set("authData", $data);
     parent::save();
     return $this;
 }