1: <?php
2: namespace Dropbox;
3:
4: /**
5: * Configure how you plan to connect to the Dropbox API (your app information, your user's
6: * locale, etc).
7: */
8: final class Config
9: {
10: /**
11: * Whatever AppInfo was passed into the constructor.
12: *
13: * @return AppInfo
14: */
15: function getAppInfo() { return $this->appInfo; }
16:
17: /** @var AppInfo */
18: private $appInfo;
19:
20: /**
21: * An identifier for the API client, typically of the form "Name/Version".
22: * This is used to set the HTTP <code>User-Agent</code> header when making API requests.
23: * Example: <code>"PhotoEditServer/1.3"</code>
24: *
25: * If you're the author a higher-level library on top of the basic SDK, and the
26: * "Photo Edit" app's server code is using your library to access Dropbox, you should append
27: * your library's name and version to form the full identifier. For example,
28: * if your library is called "File Picker", you might set this field to:
29: * <code>"PhotoEditServer/1.3 FilePicker/0.1-beta"</code>
30: *
31: * The exact format of the <code>User-Agent</code> header is described in
32: * <a href="http://tools.ietf.org/html/rfc2616#section-3.8">section 3.8 of the HTTP specification</a>.
33: *
34: * Note that underlying HTTP client may append other things to the <code>User-Agent</code>, such as
35: * the name of the library being used to actually make the HTTP request (such as cURL).
36: *
37: * @return string
38: */
39: function getClientIdentifier() { return $this->clientIdentifier; }
40:
41: /** @var string */
42: private $clientIdentifier;
43:
44: private static $dropboxSupportedLocales = array('en', 'de', 'fr', 'es', 'jp');
45:
46: /**
47: * Given a locale string, returns the closest supported locale that the Dropbox servers
48: * support. You can then use that locale string as an argument to the constructor.
49: *
50: * If you omit the $locale argument (or pass in null), we'll try using the default locale
51: * from {@link Locale::getDefault()}.
52: *
53: * @param null|string $locale
54: * @return string
55: */
56: static function getClosestSupportedLocale($locale = null)
57: {
58: if ($locale === null) {
59: $locale = setlocale(LC_ALL, 0);
60: if ($locale === false) return "en";
61: }
62:
63: $parts = preg_split('[-_]', $locale, 1);
64: $languageOnly = strtolower($parts[0]);
65:
66: if (in_array($languageOnly, self::$dropboxSupportedLocales)) {
67: return $languageOnly;
68: } else {
69: return "en";
70: }
71: }
72:
73: /**
74: * The locale of the user of your application. Some API calls return localized
75: * data and error messages; this "user locale" setting determines which locale
76: * the server should use to localize those strings.
77: *
78: * @return string
79: */
80: function getUserLocale() { return $this->userLocale; }
81:
82: /** @var string */
83: private $userLocale;
84:
85: /**
86: * Constructor.
87: *
88: * @param AppInfo $appInfo
89: * See {@link getAppInfo()}
90: * @param string $clientIdentifier
91: * See {@link getClientIdentifier()}
92: * @param null|string $userLocale
93: * See {@link getUseLocale()}
94: */
95: function __construct($appInfo, $clientIdentifier, $userLocale = null)
96: {
97: AppInfo::checkArg("appInfo", $appInfo);
98: Checker::argStringNonEmpty("clientIdentifier", $clientIdentifier);
99: Checker::argStringNonEmptyOrNull("userLocale", $userLocale);
100:
101: $this->appInfo = $appInfo;
102: $this->clientIdentifier = $clientIdentifier;
103: $this->userLocale = self::getClosestSupportedLocale($userLocale);
104: }
105:
106: /**
107: * Check that a function argument is of type <code>Config</code>.
108: *
109: * @internal
110: */
111: static function checkArg($argName, $argValue)
112: {
113: if (!($argValue instanceof self)) Checker::throwError($argName, $argValue, __CLASS__);
114: }
115:
116: /**
117: * Use this to check that a function argument is either <code>null</code> or of type
118: * <code>Config</code>.
119: *
120: * @internal
121: */
122: static function checkArgOrNull($argName, $argValue)
123: {
124: if ($argValue === null) return;
125: if (!($argValue instanceof self)) Checker::throwError($argName, $argValue, __CLASS__);
126: }
127: }
128: