Zend_Http_UserAgent-Device.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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.get-browser">
  169. <term>
  170. <methodsynopsis>
  171. <methodname>getBrowser</methodname>
  172. </methodsynopsis>
  173. </term>
  174. <listitem>
  175. <para>
  176. Returns the browser string as discoverd from the User-Agent string.
  177. </para>
  178. </listitem>
  179. </varlistentry>
  180. <varlistentry id="zend.http.user-agent-device.methods.get-browser-version">
  181. <term>
  182. <methodsynopsis>
  183. <methodname>getBrowserVersion</methodname>
  184. </methodsynopsis>
  185. </term>
  186. <listitem>
  187. <para>
  188. Retrieve the browser version as discovered from the User-Agent string.
  189. </para>
  190. </listitem>
  191. </varlistentry>
  192. <varlistentry id="zend.http.user-agent-device.methods.get-group">
  193. <term>
  194. <methodsynopsis>
  195. <methodname>getGroup</methodname>
  196. <methodparam>
  197. <funcparams>$group</funcparams>
  198. </methodparam>
  199. </methodsynopsis>
  200. </term>
  201. <listitem>
  202. <para>
  203. Get all features from a given feature group.
  204. </para>
  205. </listitem>
  206. </varlistentry>
  207. <varlistentry id="zend.http.user-agent-device.methods.get-image-format-support">
  208. <term>
  209. <methodsynopsis>
  210. <methodname>getImageFormatSupport</methodname>
  211. </methodsynopsis>
  212. </term>
  213. <listitem>
  214. <para>
  215. Retrieve a list of supported image types.
  216. </para>
  217. </listitem>
  218. </varlistentry>
  219. <varlistentry id="zend.http.user-agent-device.methods.get-images">
  220. <term>
  221. <methodsynopsis>
  222. <methodname>getImages</methodname>
  223. </methodsynopsis>
  224. </term>
  225. <listitem>
  226. <para>
  227. Alias for <methodname>getImageFormatSupport()</methodname>.
  228. </para>
  229. </listitem>
  230. </varlistentry>
  231. <varlistentry id="zend.http.user-agent-device.methods.get-max-image-height">
  232. <term>
  233. <methodsynopsis>
  234. <methodname>getMaxImageHeight</methodname>
  235. </methodsynopsis>
  236. </term>
  237. <listitem>
  238. <para>
  239. Retrieve the maximum supported image height for the current device instance.
  240. </para>
  241. </listitem>
  242. </varlistentry>
  243. <varlistentry id="zend.http.user-agent-device.methods.get-max-image-width">
  244. <term>
  245. <methodsynopsis>
  246. <methodname>getMaxImageWidth</methodname>
  247. </methodsynopsis>
  248. </term>
  249. <listitem>
  250. <para>
  251. Retrieve the maximum supported image width for the current device instance.
  252. </para>
  253. </listitem>
  254. </varlistentry>
  255. <varlistentry id="zend.http.user-agent-device.methods.get-physical-screen-height">
  256. <term>
  257. <methodsynopsis>
  258. <methodname>getPhysicalScreenHeight</methodname>
  259. </methodsynopsis>
  260. </term>
  261. <listitem>
  262. <para>
  263. Retrieve the physical screen height for the current device instance.
  264. </para>
  265. </listitem>
  266. </varlistentry>
  267. <varlistentry id="zend.http.user-agent-device.methods.get-physical-screen-width">
  268. <term>
  269. <methodsynopsis>
  270. <methodname>getPhysicalScreenWidth</methodname>
  271. </methodsynopsis>
  272. </term>
  273. <listitem>
  274. <para>
  275. Retrieve the physical screen width for the current device instance.
  276. </para>
  277. </listitem>
  278. </varlistentry>
  279. <varlistentry id="zend.http.user-agent-device.methods.get-preferred-markup">
  280. <term>
  281. <methodsynopsis>
  282. <methodname>getPreferredMarkup</methodname>
  283. </methodsynopsis>
  284. </term>
  285. <listitem>
  286. <para>
  287. Retrieve the preferred markup format for the current device instance.
  288. </para>
  289. </listitem>
  290. </varlistentry>
  291. <varlistentry id="zend.http.user-agent-device.methods.get-user-agent">
  292. <term>
  293. <methodsynopsis>
  294. <methodname>getUserAgent</methodname>
  295. </methodsynopsis>
  296. </term>
  297. <listitem>
  298. <para>
  299. Retrieve the User-Agent string for the current device instance.
  300. </para>
  301. </listitem>
  302. </varlistentry>
  303. <varlistentry id="zend.http.user-agent-device.methods.get-xhtml-support-level">
  304. <term>
  305. <methodsynopsis>
  306. <methodname>getXhtmlSupportLevel</methodname>
  307. </methodsynopsis>
  308. </term>
  309. <listitem>
  310. <para>
  311. Retrieve the supported X/HTML version for the current device instance.
  312. </para>
  313. </listitem>
  314. </varlistentry>
  315. <varlistentry id="zend.http.user-agent-device.methods.has-flash-support">
  316. <term>
  317. <methodsynopsis>
  318. <methodname>hasFlashSupport</methodname>
  319. </methodsynopsis>
  320. </term>
  321. <listitem>
  322. <para>
  323. Determine if the current device instance supports Flash.
  324. </para>
  325. </listitem>
  326. </varlistentry>
  327. <varlistentry id="zend.http.user-agent-device.methods.has-pdf-support">
  328. <term>
  329. <methodsynopsis>
  330. <methodname>hasPdfSupport</methodname>
  331. </methodsynopsis>
  332. </term>
  333. <listitem>
  334. <para>
  335. Determine if the current device instance supports PDF.
  336. </para>
  337. </listitem>
  338. </varlistentry>
  339. <varlistentry id="zend.http.user-agent-device.methods.has-phone-number">
  340. <term>
  341. <methodsynopsis>
  342. <methodname>hasPhoneNumber</methodname>
  343. </methodsynopsis>
  344. </term>
  345. <listitem>
  346. <para>
  347. Determine if the device has an associated phone number. Note: does not
  348. retrieve the phone number. This can be useful for determining if the device
  349. is a mobile phone versus another wireless capable device.
  350. </para>
  351. </listitem>
  352. </varlistentry>
  353. <varlistentry id="zend.http.user-agent-device.methods.https-support">
  354. <term>
  355. <methodsynopsis>
  356. <methodname>httpsSupport</methodname>
  357. </methodsynopsis>
  358. </term>
  359. <listitem>
  360. <para>
  361. Determine if the current device instance supports HTTPS.
  362. </para>
  363. </listitem>
  364. </varlistentry>
  365. </variablelist>
  366. </sect2>
  367. <sect2 id="zend.http.user-agent-device.examples">
  368. <title>Examples</title>
  369. <example id="zend.http.user-agent-device.examples.support">
  370. <title>Determining Supported Features</title>
  371. <para>
  372. You may wish to direct the user to specific areas of your site based on features
  373. supported by the device they are using. For instance, if a particular app works only
  374. in Flash, you might direct a non-Flash-capable device to a page indicating the
  375. application will not work on their device; or for a device not capable of HTTPS, you
  376. may indicate certain actions, such as purchases, are not available.
  377. </para>
  378. <programlisting language="php"><![CDATA[
  379. $userAgent = new Zend_Http_UserAgent();
  380. $device = $userAgent->getDevice();
  381. // Redirect to a Flash version of the app:
  382. if ($device->hasFlashSupport()) {
  383. header('Location: /flash/app');
  384. exit;
  385. }
  386. // Determine whether to show a "purchase" link:
  387. if ($device->httpsSupport()) {
  388. echo '<a href="https://store-site.com/purchase">Purchase!</a>';
  389. } else {
  390. echo 'Purchasing is unavailable on this device.';
  391. }
  392. ]]></programlisting>
  393. </example>
  394. <example id="zend.http.user-agent-device.examples.images">
  395. <title>Dynamically Scaling Images</title>
  396. <para>
  397. You may wish to alter the image sizes present in order to achieve a specific design
  398. within mobile devices. You may use a variety of methods in the device object to make
  399. this happen.
  400. </para>
  401. <programlisting language="php"><![CDATA[
  402. $userAgent = new Zend_Http_UserAgent();
  403. $device = $userAgent->getDevice();
  404. // Get the maximum image width and height
  405. $maxWidth = $device->getMaxImageWidth();
  406. $maxHeight = $device->getMaxImageHeight();
  407. // Create an <img> tag with appropriate sizes
  408. echo '<img src="/images/foo.png"';
  409. if ((null !== $maxWidth) && ($maxWidth < 328)) {
  410. echo ' width="' . $maxWidth . '"';
  411. }
  412. if ((null !== $maxHeight) && ($maxHeight < 400)) {
  413. echo ' height="' . $maxHeight . '"';
  414. }
  415. echo '/>';
  416. ]]></programlisting>
  417. </example>
  418. <note>
  419. <title>Markup- based scaling is not ideal</title>
  420. <para>
  421. Markup-based scaling such as in the example above is not the best approach, as
  422. it means that the full-sized image is still delivered to the device. A better
  423. approach is to pre-scale your images to a variety of sizes representing the
  424. devices you wish to support, and then using the device capabilities to determine
  425. which image to use.
  426. </para>
  427. <para>
  428. Another approach is to use third-party services. Zend Framework provides one
  429. such capability via the <link
  430. linkend="zend.view.helpers.initial.tiny-src">TinySrc view helper</link>.
  431. </para>
  432. </note>
  433. </sect2>
  434. </sect1>