1: <?php
2: namespace Dropbox;
3:
4: 5: 6: 7: 8: 9:
10: final class AuthInfo
11: {
12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: static function loadFromJsonFile($path)
24: {
25: if (!file_exists($path)) {
26: throw new AuthInfoLoadException("File doesn't exist: \"$path\"");
27: }
28:
29: $str = file_get_contents($path);
30: $jsonArr = json_decode($str, TRUE);
31:
32: if (is_null($jsonArr)) {
33: throw new AuthInfoLoadException("JSON parse error: \"$path\"");
34: }
35:
36: return self::loadFromJson($jsonArr);
37: }
38:
39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
50: private static function loadFromJson($jsonArr)
51: {
52: if (!is_array($jsonArr)) {
53: throw new AuthInfoLoadException("Expecting JSON object, found something else");
54: }
55:
56: if (!isset($jsonArr['app'])) {
57: throw new AuthInfoLoadException("Missing field \"app\"");
58: }
59:
60:
61: $appJson = $jsonArr['app'];
62:
63: try {
64: $appInfo = AppInfo::loadFromJson($appJson);
65: }
66: catch (AppInfoLoadException $e) {
67: throw new AuthInfoLoadException("Bad \"app\" field: ".$e->getMessage());
68: }
69:
70:
71: if (!isset($jsonArr['access_token'])) {
72: throw new AuthInfoLoadException("Missing field \"access_token\"");
73: }
74:
75: $accessTokenString = $jsonArr['access_token'];
76: if (!is_string($accessTokenString)) {
77: throw new AuthInfoLoadException("Expecting field \"access_token\" to be a string");
78: }
79:
80: $accessToken = AccessToken::deserialize($accessTokenString);
81:
82: return array($appInfo, $accessToken);
83: }
84: }
85: