public function testMetaData() { $file = LeanFile::createWithData("test.txt", "你好,中国!"); $file->setMeta("language", "zh-CN"); $file->setMeta("bool", false); $file->setMeta("downloads", 100); $file->save(); $file2 = LeanFile::fetch($file->getObjectId()); $this->assertEquals("zh-CN", $file2->getMeta("language")); $this->assertEquals(false, $file2->getMeta("bool")); $this->assertEquals(100, $file2->getMeta("downloads")); $file->destroy(); }
/** * 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"]); } }
public function testCircularGetCurrentUser() { // ensure getCurrentUser neither run indefinetely, nor throw maximum // function all error $avatar = LeanFile::createWithUrl("alice.png", "https://leancloud.cn/favicon.png"); $user = LeanUser::logIn("alice", "blabla"); $user->set("avatar", $avatar); $user->save(); $token = LeanUser::getCurrentSessionToken(); $user->logOut(); LeanUser::setCurrentSessionToken($token); $user2 = LeanUser::getCurrentUser(); $this->assertEquals($user2->getUsername(), "alice"); }
public function testSaveObjectWithFile() { $obj = new LeanObject("TestObject"); $obj->set("name", "alice"); $file = LeanFile::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(); }
/** * Fetch file object by id * * Note it fetches descriptive data from LeanCloud, but not file content. * The content should be fetched from file URL. * * @return LeanFile */ public static function fetch($objectId) { $file = new LeanFile(""); $resp = LeanClient::get("/files/{$objectId}"); $file->mergeAfterFetch($resp); return $file; }
/** * 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"]); } }