/**
  * Parses a JSON object to build an AuthInfo object.  If you would like to load this from a file,
  * please use the @see loadFromJsonFile method.
  *
  * @param array $jsonArr
  *    A parsed JSON object, typcally the result of json_decode(..., true).
  * @return array
  *    A <code>list(string $accessToken, Host $host)</code>.
  *
  * @throws AuthInfoLoadException
  */
 private static function loadFromJson($jsonArr)
 {
     if (!is_array($jsonArr)) {
         throw new AuthInfoLoadException("Expecting JSON object, found something else");
     }
     // Check access_token
     if (!array_key_exists('access_token', $jsonArr)) {
         throw new AuthInfoLoadException("Missing field \"access_token\"");
     }
     $accessToken = $jsonArr['access_token'];
     if (!is_string($accessToken)) {
         throw new AuthInfoLoadException("Expecting field \"access_token\" to be a string");
     }
     try {
         $host = Host::loadFromJson($jsonArr);
     } catch (HostLoadException $ex) {
         throw new AuthInfoLoadException($ex->getMessage());
     }
     return array($accessToken, $host);
 }
Exemple #2
0
 /**
  * Parses a JSON object to build an AppInfo object.  If you would like to load this from a file,
  * use the loadFromJsonFile() method.
  *
  * @param array $jsonArr Output from json_decode($str, true)
  *
  * @return AppInfo
  *
  * @throws AppInfoLoadException
  */
 static function loadFromJson($jsonArr)
 {
     if (!is_array($jsonArr)) {
         throw new AppInfoLoadException("Expecting JSON object, got something else");
     }
     $requiredKeys = array("key", "secret");
     foreach ($requiredKeys as $key) {
         if (!array_key_exists($key, $jsonArr)) {
             throw new AppInfoLoadException("Missing field \"{$key}\"");
         }
         if (!is_string($jsonArr[$key])) {
             throw new AppInfoLoadException("Expecting field \"{$key}\" to be a string");
         }
     }
     // Check app_key and app_secret
     $appKey = $jsonArr["key"];
     $appSecret = $jsonArr["secret"];
     $tokenErr = self::getTokenPartError($appKey);
     if (!is_null($tokenErr)) {
         throw new AppInfoLoadException("Field \"key\" doesn't look like a valid app key: {$tokenErr}");
     }
     $tokenErr = self::getTokenPartError($appSecret);
     if (!is_null($tokenErr)) {
         throw new AppInfoLoadException("Field \"secret\" doesn't look like a valid app secret: {$tokenErr}");
     }
     try {
         $host = Host::loadFromJson($jsonArr);
     } catch (HostLoadException $ex) {
         throw new AppInfoLoadException($ex->getMessage());
     }
     return new AppInfo($appKey, $appSecret, $host);
 }