Zend_Http_UserAgent-Device.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.http.user-agent-device">
  4. <title>The UserAgent Device Interface</title>
  5. <sect2 id="zend.http.user-agent-device.intro">
  6. <title>Overview</title>
  7. <para>
  8. Once the User-Agent has been parsed and capabilities retrieved from the <link
  9. linkend="zend.http.user-agent-features">Features adapter</link>, you will be
  10. returned an object that represents the discovered brower device. This interface
  11. describes the various capabilities you may now introspect.
  12. </para>
  13. <para>
  14. Additionally, the various device classes define algorithms for matching the devices they
  15. describe. By implementing this interface, you may provide additional logic around these
  16. capabilities.
  17. </para>
  18. </sect2>
  19. <sect2 id="zend.http.user-agent-device.quick-start">
  20. <title>Quick Start</title>
  21. <para>
  22. The <interfacename>Zend_Http_UserAgent_Device</interfacename> interface defines the
  23. following methods.
  24. </para>
  25. <programlisting language="php"><![CDATA[
  26. interface Zend_Http_UserAgent_Device extends Serializable
  27. {
  28. public function __construct($userAgent = null, array $server = array(), array $config = array());
  29. public static function match($userAgent, $server);
  30. public function getAllFeatures();
  31. public function getAllGroups();
  32. public function getBrowser();
  33. public function getBrowserVersion();
  34. public function getGroup($group);
  35. public function getImageFormatSupport();
  36. public function getImages();
  37. public function getMaxImageHeight();
  38. public function getMaxImageWidth();
  39. public function getPhysicalScreenHeight();
  40. public function getPhysicalScreenWidth();
  41. public function getPreferredMarkup();
  42. public function getUserAgent();
  43. public function getXhtmlSupportLevel();
  44. public function hasFlashSupport();
  45. public function hasPdfSupport();
  46. public function hasPhoneNumber();
  47. public function httpsSupport();
  48. }
  49. ]]></programlisting>
  50. <para>
  51. The static function <methodname>match()</methodname> should be used to determine whether
  52. the provided User-Agent and environment (represented by the <varname>$server</varname>
  53. variable) match this device. If they do, the <classname>Zend_Http_UserAgent</classname>
  54. class will then create an instance of the class, passing it the User-Agent,
  55. <varname>$server</varname> array, and any configuration available; at this time, it is
  56. expected that the Device class will consult with a features adapter, if present, and
  57. populate itself with discovered capabilities.
  58. </para>
  59. <para>
  60. In practice, you will likely extend
  61. <classname>Zend_Http_UserAgent_AbstractDevice</classname>, which provides functionality
  62. around discovering capabilities from the User-Agent string itself, as well as
  63. discovering and querying a <link linkend="zend.http.user-agent-features">Features
  64. adapter</link>.
  65. </para>
  66. </sect2>
  67. <sect2 id="zend.http.user-agent-device.options">
  68. <title>Configuration Options</title>
  69. <para>
  70. Configuration options are defined on a per-device basis. The following options are
  71. defined in <classname>Zend_Http_UserAgent_AbstractDevice</classname>. Like all options,
  72. the "." character represents an additional level of depth to the configuration array.
  73. </para>
  74. <variablelist>
  75. <title>Device Options</title>
  76. <varlistentry>
  77. <term>features.classname</term>
  78. <listitem>
  79. <para>
  80. The name of a <link linkend="zend.http.user-agent-features">Features
  81. adapter</link> class that has either been previously loaded or which is
  82. discoverable via autoloading, or used in conjunction with the
  83. <varname>features.path</varname> option. This class must implement the
  84. <interfacename>Zend_Http_UserAgent_Features_Adapter</interfacename>
  85. interface.
  86. </para>
  87. </listitem>
  88. </varlistentry>
  89. <varlistentry>
  90. <term>features.path</term>
  91. <listitem>
  92. <para>
  93. If provided, the filesystem path to the features adapter class being used.
  94. The path must either be an absolute path or discoverable via the
  95. <varname>include_path</varname>.
  96. </para>
  97. </listitem>
  98. </varlistentry>
  99. </variablelist>
  100. </sect2>
  101. <sect2 id="zend.http.user-agent-device.methods">
  102. <title>Available Methods</title>
  103. <variablelist>
  104. <varlistentry id="zend.http.user-agent-device.methods.constructor">
  105. <term>
  106. <methodsynopsis>
  107. <methodname>__construct</methodname>
  108. <methodparam>
  109. <funcparams>$userAgent = null, array $server = array(), array $config =
  110. array()</funcparams>
  111. </methodparam>
  112. </methodsynopsis>
  113. </term>
  114. <listitem>
  115. <para>
  116. Expects a User-Agent string, an array representing the HTTP environment, and an
  117. array of configuration values. The values are all optional, as they may be
  118. populated during deserialization.
  119. </para>
  120. </listitem>
  121. </varlistentry>
  122. <varlistentry id="zend.http.user-agent-device.methods.match">
  123. <term>
  124. <methodsynopsis>
  125. <methodname>match</methodname>
  126. <methodparam>
  127. <funcparams>$userAgent, $server</funcparams>
  128. </methodparam>
  129. </methodsynopsis>
  130. </term>
  131. <listitem>
  132. <para>
  133. This method is static.
  134. </para>
  135. <para>
  136. Provided the <varname>$userAgent</varname> string and an array representing the
  137. HTTP headers provided in the request, determine if they match the capabilities
  138. of this device. This method should return a boolean.
  139. </para>
  140. </listitem>
  141. </varlistentry>
  142. <varlistentry id="zend.http.user-agent-device.methods.get-all-features">
  143. <term>
  144. <methodsynopsis>
  145. <methodname>getAllFeatures</methodname>
  146. </methodsynopsis>
  147. </term>
  148. <listitem>
  149. <para>
  150. Returns an array of key/value pairs representing the features supported by
  151. this device instance.
  152. </para>
  153. </listitem>
  154. </varlistentry>
  155. <varlistentry id="zend.http.user-agent-device.methods.get-all-groups">
  156. <term>
  157. <methodsynopsis>
  158. <methodname>getAllGroups</methodname>
  159. </methodsynopsis>
  160. </term>
  161. <listitem>
  162. <para>
  163. Similar to <methodname>getAllFeatures()</methodname>, this retrieves all
  164. features, but grouped by type.
  165. </para>
  166. </listitem>
  167. </varlistentry>
  168. <varlistentry id="zend.http.user-agent-device.methods.has-feature">
  169. <term>
  170. <methodsynopsis>
  171. <methodname>hasFeature</methodname>
  172. <methodparam>
  173. <funcparams>$feature</funcparams>
  174. </methodparam>
  175. </methodsynopsis>
  176. </term>
  177. <listitem>
  178. <para>
  179. Query the device to determine if it contains information on a specific
  180. feature.
  181. </para>
  182. </listitem>
  183. </varlistentry>
  184. <varlistentry id="zend.http.user-agent-device.methods.get-feature">
  185. <term>
  186. <methodsynopsis>
  187. <methodname>getFeature</methodname>
  188. <methodparam>
  189. <funcparams>$feature</funcparams>
  190. </methodparam>
  191. </methodsynopsis>
  192. </term>
  193. <listitem>
  194. <para>
  195. Retrieve the value of a specific device feature, if present. Typically, this
  196. will return a boolean <constant>false</constant> or a
  197. <constant>null</constant> value if the feature is not defined.
  198. </para>
  199. </listitem>
  200. </varlistentry>
  201. <varlistentry id="zend.http.user-agent-device.methods.get-browser">
  202. <term>
  203. <methodsynopsis>
  204. <methodname>getBrowser</methodname>
  205. </methodsynopsis>
  206. </term>
  207. <listitem>
  208. <para>
  209. Returns the browser string as discoverd from the User-Agent string.
  210. </para>
  211. </listitem>
  212. </varlistentry>
  213. <varlistentry id="zend.http.user-agent-device.methods.get-browser-version">
  214. <term>
  215. <methodsynopsis>
  216. <methodname>getBrowserVersion</methodname>
  217. </methodsynopsis>
  218. </term>
  219. <listitem>
  220. <para>
  221. Retrieve the browser version as discovered from the User-Agent string.
  222. </para>
  223. </listitem>
  224. </varlistentry>
  225. <varlistentry id="zend.http.user-agent-device.methods.get-group">
  226. <term>
  227. <methodsynopsis>
  228. <methodname>getGroup</methodname>
  229. <methodparam>
  230. <funcparams>$group</funcparams>
  231. </methodparam>
  232. </methodsynopsis>
  233. </term>
  234. <listitem>
  235. <para>
  236. Get all features from a given feature group.
  237. </para>
  238. </listitem>
  239. </varlistentry>
  240. <varlistentry id="zend.http.user-agent-device.methods.get-image-format-support">
  241. <term>
  242. <methodsynopsis>
  243. <methodname>getImageFormatSupport</methodname>
  244. </methodsynopsis>
  245. </term>
  246. <listitem>
  247. <para>
  248. Retrieve a list of supported image types.
  249. </para>
  250. </listitem>
  251. </varlistentry>
  252. <varlistentry id="zend.http.user-agent-device.methods.get-images">
  253. <term>
  254. <methodsynopsis>
  255. <methodname>getImages</methodname>
  256. </methodsynopsis>
  257. </term>
  258. <listitem>
  259. <para>
  260. Alias for <methodname>getImageFormatSupport()</methodname>.
  261. </para>
  262. </listitem>
  263. </varlistentry>
  264. <varlistentry id="zend.http.user-agent-device.methods.get-max-image-height">
  265. <term>
  266. <methodsynopsis>
  267. <methodname>getMaxImageHeight</methodname>
  268. </methodsynopsis>
  269. </term>
  270. <listitem>
  271. <para>
  272. Retrieve the maximum supported image height for the current device instance.
  273. </para>
  274. </listitem>
  275. </varlistentry>
  276. <varlistentry id="zend.http.user-agent-device.methods.get-max-image-width">
  277. <term>
  278. <methodsynopsis>
  279. <methodname>getMaxImageWidth</methodname>
  280. </methodsynopsis>
  281. </term>
  282. <listitem>
  283. <para>
  284. Retrieve the maximum supported image width for the current device instance.
  285. </para>
  286. </listitem>
  287. </varlistentry>
  288. <varlistentry id="zend.http.user-agent-device.methods.get-physical-screen-height">
  289. <term>
  290. <methodsynopsis>
  291. <methodname>getPhysicalScreenHeight</methodname>
  292. </methodsynopsis>
  293. </term>
  294. <listitem>
  295. <para>
  296. Retrieve the physical screen height for the current device instance.
  297. </para>
  298. </listitem>
  299. </varlistentry>
  300. <varlistentry id="zend.http.user-agent-device.methods.get-physical-screen-width">
  301. <term>
  302. <methodsynopsis>
  303. <methodname>getPhysicalScreenWidth</methodname>
  304. </methodsynopsis>
  305. </term>
  306. <listitem>
  307. <para>
  308. Retrieve the physical screen width for the current device instance.
  309. </para>
  310. </listitem>
  311. </varlistentry>
  312. <varlistentry id="zend.http.user-agent-device.methods.get-preferred-markup">
  313. <term>
  314. <methodsynopsis>
  315. <methodname>getPreferredMarkup</methodname>
  316. </methodsynopsis>
  317. </term>
  318. <listitem>
  319. <para>
  320. Retrieve the preferred markup format for the current device instance.
  321. </para>
  322. </listitem>
  323. </varlistentry>
  324. <varlistentry id="zend.http.user-agent-device.methods.get-user-agent">
  325. <term>
  326. <methodsynopsis>
  327. <methodname>getUserAgent</methodname>
  328. </methodsynopsis>
  329. </term>
  330. <listitem>
  331. <para>
  332. Retrieve the User-Agent string for the current device instance.
  333. </para>
  334. </listitem>
  335. </varlistentry>
  336. <varlistentry id="zend.http.user-agent-device.methods.get-xhtml-support-level">
  337. <term>
  338. <methodsynopsis>
  339. <methodname>getXhtmlSupportLevel</methodname>
  340. </methodsynopsis>
  341. </term>
  342. <listitem>
  343. <para>
  344. Retrieve the supported X/HTML version for the current device instance.
  345. </para>
  346. </listitem>
  347. </varlistentry>
  348. <varlistentry id="zend.http.user-agent-device.methods.has-flash-support">
  349. <term>
  350. <methodsynopsis>
  351. <methodname>hasFlashSupport</methodname>
  352. </methodsynopsis>
  353. </term>
  354. <listitem>
  355. <para>
  356. Determine if the current device instance supports Flash.
  357. </para>
  358. </listitem>
  359. </varlistentry>
  360. <varlistentry id="zend.http.user-agent-device.methods.has-pdf-support">
  361. <term>
  362. <methodsynopsis>
  363. <methodname>hasPdfSupport</methodname>
  364. </methodsynopsis>
  365. </term>
  366. <listitem>
  367. <para>
  368. Determine if the current device instance supports PDF.
  369. </para>
  370. </listitem>
  371. </varlistentry>
  372. <varlistentry id="zend.http.user-agent-device.methods.has-phone-number">
  373. <term>
  374. <methodsynopsis>
  375. <methodname>hasPhoneNumber</methodname>
  376. </methodsynopsis>
  377. </term>
  378. <listitem>
  379. <para>
  380. Determine if the device has an associated phone number. Note: does not
  381. retrieve the phone number. This can be useful for determining if the device
  382. is a mobile phone versus another wireless capable device.
  383. </para>
  384. </listitem>
  385. </varlistentry>
  386. <varlistentry id="zend.http.user-agent-device.methods.https-support">
  387. <term>
  388. <methodsynopsis>
  389. <methodname>httpsSupport</methodname>
  390. </methodsynopsis>
  391. </term>
  392. <listitem>
  393. <para>
  394. Determine if the current device instance supports HTTPS.
  395. </para>
  396. </listitem>
  397. </varlistentry>
  398. </variablelist>
  399. </sect2>
  400. <sect2 id="zend.http.user-agent-device.examples">
  401. <title>Examples</title>
  402. <example id="zend.http.user-agent-device.examples.support">
  403. <title>Determining Supported Features</title>
  404. <para>
  405. You may wish to direct the user to specific areas of your site based on features
  406. supported by the device they are using. For instance, if a particular app works only
  407. in Flash, you might direct a non-Flash-capable device to a page indicating the
  408. application will not work on their device; or for a device not capable of HTTPS, you
  409. may indicate certain actions, such as purchases, are not available.
  410. </para>
  411. <programlisting language="php"><![CDATA[
  412. $userAgent = new Zend_Http_UserAgent();
  413. $device = $userAgent->getDevice();
  414. // Redirect to a Flash version of the app:
  415. if ($device->hasFlashSupport()) {
  416. header('Location: /flash/app');
  417. exit;
  418. }
  419. // Determine whether to show a "purchase" link:
  420. if ($device->httpsSupport()) {
  421. echo '<a href="https://store-site.com/purchase">Purchase!</a>';
  422. } else {
  423. echo 'Purchasing is unavailable on this device.';
  424. }
  425. ]]></programlisting>
  426. </example>
  427. <example id="zend.http.user-agent-device.examples.images">
  428. <title>Dynamically Scaling Images</title>
  429. <para>
  430. You may wish to alter the image sizes present in order to achieve a specific design
  431. within mobile devices. You may use a variety of methods in the device object to make
  432. this happen.
  433. </para>
  434. <programlisting language="php"><![CDATA[
  435. $userAgent = new Zend_Http_UserAgent();
  436. $device = $userAgent->getDevice();
  437. // Get the maximum image width and height
  438. $maxWidth = $device->getMaxImageWidth();
  439. $maxHeight = $device->getMaxImageHeight();
  440. // Create an <img> tag with appropriate sizes
  441. echo '<img src="/images/foo.png"';
  442. if ((null !== $maxWidth) && ($maxWidth < 328)) {
  443. echo ' width="' . $maxWidth . '"';
  444. }
  445. if ((null !== $maxHeight) && ($maxHeight < 400)) {
  446. echo ' height="' . $maxHeight . '"';
  447. }
  448. echo '/>';
  449. ]]></programlisting>
  450. </example>
  451. <note>
  452. <title>Markup- based scaling is not ideal</title>
  453. <para>
  454. Markup-based scaling such as in the example above is not the best approach, as
  455. it means that the full-sized image is still delivered to the device. A better
  456. approach is to pre-scale your images to a variety of sizes representing the
  457. devices you wish to support, and then using the device capabilities to determine
  458. which image to use.
  459. </para>
  460. </note>
  461. </sect2>
  462. </sect1>