Zend_Http_UserAgent-Device.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. <refentry id="zend.http.user-agent-device.methods.constructor">
  104. <refnamediv>
  105. <refname>__construct</refname>
  106. <refpurpose>Instantiate a Device instance</refpurpose>
  107. </refnamediv>
  108. <refsynopsisdiv>
  109. <methodsynopsis>
  110. <methodname>__construct</methodname>
  111. <methodparam>
  112. <funcparams>$userAgent = null, array $server = array(), array $config = array()</funcparams>
  113. </methodparam>
  114. </methodsynopsis>
  115. </refsynopsisdiv>
  116. <refsect1>
  117. <title>__construct()</title>
  118. <para>
  119. Expects a User-Agent string, an array representing the HTTP environment, and an
  120. array of configuration values. The values are all optional, as they may be
  121. populated during deserialization.
  122. </para>
  123. </refsect1>
  124. </refentry>
  125. <refentry id="zend.http.user-agent-device.methods.match">
  126. <refnamediv>
  127. <refname>match</refname>
  128. <refpurpose>Determine if a User-Agent matches this device</refpurpose>
  129. </refnamediv>
  130. <refsynopsisdiv>
  131. <methodsynopsis>
  132. <methodname>match</methodname>
  133. <methodparam>
  134. <funcparams>$userAgent, $server</funcparams>
  135. </methodparam>
  136. </methodsynopsis>
  137. </refsynopsisdiv>
  138. <refsect1>
  139. <title>match()</title>
  140. <para>
  141. This method is static.
  142. </para>
  143. <para>
  144. Provided the <varname>$userAgent</varname> string and an array representing the
  145. HTTP headers provided in the request, determine if they match the capabilities
  146. of this device. This method should return a boolean.
  147. </para>
  148. </refsect1>
  149. </refentry>
  150. <refentry id="zend.http.user-agent-device.methods.get-all-features">
  151. <refnamediv>
  152. <refname>getAllFeatures</refname>
  153. <refpurpose>Get an array of all supported features</refpurpose>
  154. </refnamediv>
  155. <refsynopsisdiv>
  156. <methodsynopsis>
  157. <methodname>getAllFeatures</methodname>
  158. </methodsynopsis>
  159. </refsynopsisdiv>
  160. <refsect1>
  161. <title>getAllFeatures()</title>
  162. <para>
  163. Returns an array of key/value pairs representing the features supported by this
  164. device instance.
  165. </para>
  166. </refsect1>
  167. </refentry>
  168. <refentry id="zend.http.user-agent-device.methods.get-all-groups">
  169. <refnamediv>
  170. <refname>getAllGroups</refname>
  171. <refpurpose>Retrieve all feature groups</refpurpose>
  172. </refnamediv>
  173. <refsynopsisdiv>
  174. <methodsynopsis>
  175. <methodname>getAllGroups</methodname>
  176. </methodsynopsis>
  177. </refsynopsisdiv>
  178. <refsect1>
  179. <title>getAllGroups()</title>
  180. <para>
  181. Similar to <methodname>getAllFeatures()</methodname>, this retrieves all
  182. features, but grouped by type.
  183. </para>
  184. </refsect1>
  185. </refentry>
  186. <refentry id="zend.http.user-agent-device.methods.get-browser">
  187. <refnamediv>
  188. <refname>getBrowser</refname>
  189. <refpurpose>Get the User-Agent browser string</refpurpose>
  190. </refnamediv>
  191. <refsynopsisdiv>
  192. <methodsynopsis>
  193. <methodname>getBrowser</methodname>
  194. </methodsynopsis>
  195. </refsynopsisdiv>
  196. <refsect1>
  197. <title>getBrowser()</title>
  198. <para>
  199. Returns the browser string as discoverd from the User-Agent string.
  200. </para>
  201. </refsect1>
  202. </refentry>
  203. <refentry id="zend.http.user-agent-device.methods.get-browser-version">
  204. <refnamediv>
  205. <refname>getBrowserVersion</refname>
  206. <refpurpose>Determine the browser version</refpurpose>
  207. </refnamediv>
  208. <refsynopsisdiv>
  209. <methodsynopsis>
  210. <methodname>getBrowserVersion</methodname>
  211. </methodsynopsis>
  212. </refsynopsisdiv>
  213. <refsect1>
  214. <title>getBrowserVersion()</title>
  215. <para>
  216. Retrieve the browser version as discovered from the User-Agent string.
  217. </para>
  218. </refsect1>
  219. </refentry>
  220. <refentry id="zend.http.user-agent-device.methods.get-group">
  221. <refnamediv>
  222. <refname>getGroup</refname>
  223. <refpurpose>Get features from a given group</refpurpose>
  224. </refnamediv>
  225. <refsynopsisdiv>
  226. <methodsynopsis>
  227. <methodname>getGroup</methodname>
  228. <methodparam>
  229. <funcparams>$group</funcparams>
  230. </methodparam>
  231. </methodsynopsis>
  232. </refsynopsisdiv>
  233. <refsect1>
  234. <title>getGroup()</title>
  235. <para>
  236. Get all features from a given feature group.
  237. </para>
  238. </refsect1>
  239. </refentry>
  240. <refentry id="zend.http.user-agent-device.methods.get-image-format-support">
  241. <refnamediv>
  242. <refname>getImageFormatSupport</refname>
  243. <refpurpose>Retrieve list of supported images</refpurpose>
  244. </refnamediv>
  245. <refsynopsisdiv>
  246. <methodsynopsis>
  247. <methodname>getImageFormatSupport</methodname>
  248. </methodsynopsis>
  249. </refsynopsisdiv>
  250. <refsect1>
  251. <title>getImageFormatSupport()</title>
  252. <para>
  253. Retrieve a list of supported image types.
  254. </para>
  255. </refsect1>
  256. </refentry>
  257. <refentry id="zend.http.user-agent-device.methods.get-images">
  258. <refnamediv>
  259. <refname>getImages</refname>
  260. <refpurpose>Alias for <methodname>getImageFormatSupport()</methodname></refpurpose>
  261. </refnamediv>
  262. <refsynopsisdiv>
  263. <methodsynopsis>
  264. <methodname>getImages</methodname>
  265. </methodsynopsis>
  266. </refsynopsisdiv>
  267. <refsect1>
  268. <title>getImages()</title>
  269. <para>
  270. Alias for <methodname>getImageFormatSupport()</methodname>.
  271. </para>
  272. </refsect1>
  273. </refentry>
  274. <refentry id="zend.http.user-agent-device.methods.get-max-image-height">
  275. <refnamediv>
  276. <refname>getMaxImageHeight</refname>
  277. <refpurpose>Get maximum supported image height</refpurpose>
  278. </refnamediv>
  279. <refsynopsisdiv>
  280. <methodsynopsis>
  281. <methodname>getMaxImageHeight</methodname>
  282. </methodsynopsis>
  283. </refsynopsisdiv>
  284. <refsect1>
  285. <title>getMaxImageHeight()</title>
  286. <para>
  287. Retrieve the maximum supported image height for the current device instance.
  288. </para>
  289. </refsect1>
  290. </refentry>
  291. <refentry id="zend.http.user-agent-device.methods.get-max-image-width">
  292. <refnamediv>
  293. <refname>getMaxImageWidth</refname>
  294. <refpurpose>Get maximum supported image width</refpurpose>
  295. </refnamediv>
  296. <refsynopsisdiv>
  297. <methodsynopsis>
  298. <methodname>getMaxImageWidth</methodname>
  299. </methodsynopsis>
  300. </refsynopsisdiv>
  301. <refsect1>
  302. <title>getMaxImageWidth()</title>
  303. <para>
  304. Retrieve the maximum supported image width for the current device instance.
  305. </para>
  306. </refsect1>
  307. </refentry>
  308. <refentry id="zend.http.user-agent-device.methods.get-physical-screen-height">
  309. <refnamediv>
  310. <refname>getPhysicalScreenHeight</refname>
  311. <refpurpose>Get the physical screen height for the device</refpurpose>
  312. </refnamediv>
  313. <refsynopsisdiv>
  314. <methodsynopsis>
  315. <methodname>getPhysicalScreenHeight</methodname>
  316. </methodsynopsis>
  317. </refsynopsisdiv>
  318. <refsect1>
  319. <title>getPhysicalScreenHeight()</title>
  320. <para>
  321. Retrieve the physical screen height for the current device instance.
  322. </para>
  323. </refsect1>
  324. </refentry>
  325. <refentry id="zend.http.user-agent-device.methods.get-physical-screen-width">
  326. <refnamediv>
  327. <refname>getPhysicalScreenWidth</refname>
  328. <refpurpose>Get the physical screen width for the device</refpurpose>
  329. </refnamediv>
  330. <refsynopsisdiv>
  331. <methodsynopsis>
  332. <methodname>getPhysicalScreenWidth</methodname>
  333. </methodsynopsis>
  334. </refsynopsisdiv>
  335. <refsect1>
  336. <title>getPhysicalScreenWidth()</title>
  337. <para>
  338. Retrieve the physical screen width for the current device instance.
  339. </para>
  340. </refsect1>
  341. </refentry>
  342. <refentry id="zend.http.user-agent-device.methods.get-preferred-markup">
  343. <refnamediv>
  344. <refname>getPreferredMarkup</refname>
  345. <refpurpose>Determine the preferred markup format</refpurpose>
  346. </refnamediv>
  347. <refsynopsisdiv>
  348. <methodsynopsis>
  349. <methodname>getPreferredMarkup</methodname>
  350. </methodsynopsis>
  351. </refsynopsisdiv>
  352. <refsect1>
  353. <title>getPreferredMarkup()</title>
  354. <para>
  355. Retrieve the preferred markup format for the current device instance.
  356. </para>
  357. </refsect1>
  358. </refentry>
  359. <refentry id="zend.http.user-agent-device.methods.get-user-agent">
  360. <refnamediv>
  361. <refname>getUserAgent</refname>
  362. <refpurpose>Get the User-Agent string</refpurpose>
  363. </refnamediv>
  364. <refsynopsisdiv>
  365. <methodsynopsis>
  366. <methodname>getUserAgent</methodname>
  367. </methodsynopsis>
  368. </refsynopsisdiv>
  369. <refsect1>
  370. <title>getUserAgent()</title>
  371. <para>
  372. Retrieve the User-Agent string for the current device instance.
  373. </para>
  374. </refsect1>
  375. </refentry>
  376. <refentry id="zend.http.user-agent-device.methods.get-xhtml-support-level">
  377. <refnamediv>
  378. <refname>getXhtmlSupportLevel</refname>
  379. <refpurpose>Get the version of XHTML supported</refpurpose>
  380. </refnamediv>
  381. <refsynopsisdiv>
  382. <methodsynopsis>
  383. <methodname>getXhtmlSupportLevel</methodname>
  384. </methodsynopsis>
  385. </refsynopsisdiv>
  386. <refsect1>
  387. <title>getXhtmlSupportLevel()</title>
  388. <para>
  389. Retrieve the supported X/HTML version for the current device instance.
  390. </para>
  391. </refsect1>
  392. </refentry>
  393. <refentry id="zend.http.user-agent-device.methods.has-flash-support">
  394. <refnamediv>
  395. <refname>hasFlashSupport</refname>
  396. <refpurpose>Does the device support Flash?</refpurpose>
  397. </refnamediv>
  398. <refsynopsisdiv>
  399. <methodsynopsis>
  400. <methodname>hasFlashSupport</methodname>
  401. </methodsynopsis>
  402. </refsynopsisdiv>
  403. <refsect1>
  404. <title>hasFlashSupport()</title>
  405. <para>
  406. Determine if the current device instance supports Flash.
  407. </para>
  408. </refsect1>
  409. </refentry>
  410. <refentry id="zend.http.user-agent-device.methods.has-pdf-support">
  411. <refnamediv>
  412. <refname>hasPdfSupport</refname>
  413. <refpurpose>Does the device support PDF?</refpurpose>
  414. </refnamediv>
  415. <refsynopsisdiv>
  416. <methodsynopsis>
  417. <methodname>hasPdfSupport</methodname>
  418. </methodsynopsis>
  419. </refsynopsisdiv>
  420. <refsect1>
  421. <title>hasPdfSupport()</title>
  422. <para>
  423. Determine if the current device instance supports PDF.
  424. </para>
  425. </refsect1>
  426. </refentry>
  427. <refentry id="zend.http.user-agent-device.methods.has-phone-number">
  428. <refnamediv>
  429. <refname>hasPhoneNumber</refname>
  430. <refpurpose>Does the device have a phone number?</refpurpose>
  431. </refnamediv>
  432. <refsynopsisdiv>
  433. <methodsynopsis>
  434. <methodname>hasPhoneNumber</methodname>
  435. </methodsynopsis>
  436. </refsynopsisdiv>
  437. <refsect1>
  438. <title>hasPhoneNumber()</title>
  439. <para>
  440. Determine if the device has an associated phone number. Note: does not retrieve
  441. the phone number. This can be useful for determining if the device is a mobile
  442. phone versus another wireless capable device.
  443. </para>
  444. </refsect1>
  445. </refentry>
  446. <refentry id="zend.http.user-agent-device.methods.https-support">
  447. <refnamediv>
  448. <refname>httpsSupport</refname>
  449. <refpurpose>Does the device support HTTPS?</refpurpose>
  450. </refnamediv>
  451. <refsynopsisdiv>
  452. <methodsynopsis>
  453. <methodname>httpsSupport</methodname>
  454. </methodsynopsis>
  455. </refsynopsisdiv>
  456. <refsect1>
  457. <title>httpsSupport()</title>
  458. <para>
  459. Determine if the current device instance supports HTTPS.
  460. </para>
  461. </refsect1>
  462. </refentry>
  463. </sect2>
  464. <sect2 id="zend.http.user-agent-device.examples">
  465. <title>Examples</title>
  466. <example id="zend.http.user-agent-device.examples.support">
  467. <title>Determining Supported Features</title>
  468. <para>
  469. You may wish to direct the user to specific areas of your site based on features
  470. supported by the device they are using. For instance, if a particular app works only
  471. in Flash, you might direct a non-Flash-capable device to a page indicating the
  472. application will not work on their device; or for a device not capable of HTTPS, you
  473. may indicate certain actions, such as purchases, are not available.
  474. </para>
  475. <programlisting language="php"><![CDATA[
  476. $userAgent = new Zend_Http_UserAgent();
  477. $device = $userAgent->getDevice();
  478. // Redirect to a Flash version of the app:
  479. if ($device->hasFlashSupport()) {
  480. header('Location: /flash/app');
  481. exit;
  482. }
  483. // Determine whether to show a "purchase" link:
  484. if ($device->httpsSupport()) {
  485. echo '<a href="https://store-site.com/purchase">Purchase!</a>';
  486. } else {
  487. echo 'Purchasing is unavailable on this device.';
  488. }
  489. ]]></programlisting>
  490. </example>
  491. <example id="zend.http.user-agent-device.examples.images">
  492. <title>Dynamically Scaling Images</title>
  493. <para>
  494. You may wish to alter the image sizes present in order to achieve a specific design
  495. within mobile devices. You may use a variety of methods in the device object to make
  496. this happen.
  497. </para>
  498. <programlisting language="php"><![CDATA[
  499. $userAgent = new Zend_Http_UserAgent();
  500. $device = $userAgent->getDevice();
  501. // Get the maximum image width and height
  502. $maxWidth = $device->getMaxImageWidth();
  503. $maxHeight = $device->getMaxImageHeight();
  504. // Create an <img> tag with appropriate sizes
  505. echo '<img src="/images/foo.png"';
  506. if ((null !== $maxWidth) && ($maxWidth < 328)) {
  507. echo ' width="' . $maxWidth . '"';
  508. }
  509. if ((null !== $maxHeight) && ($maxHeight < 400)) {
  510. echo ' height="' . $maxHeight . '"';
  511. }
  512. echo '/>';
  513. ]]></programlisting>
  514. </example>
  515. <note>
  516. <title>Markup- based scaling is not ideal</title>
  517. <para>
  518. Markup-based scaling such as in the example above is not the best approach, as
  519. it means that the full-sized image is still delivered to the device. A better
  520. approach is to pre-scale your images to a variety of sizes representing the
  521. devices you wish to support, and then using the device capabilities to determine
  522. which image to use.
  523. </para>
  524. <para>
  525. Another approach is to use third-party services. Zend Framework provides one
  526. such capability via the <link
  527. linkend="zend.view.helpers.initial.tiny-src">TinySrc view helper</link>.
  528. </para>
  529. </note>
  530. </sect2>
  531. </sect1>