The UserAgent Device Interface

Overview

Once the User-Agent has been parsed and capabilities retrieved from the Features adapter, you will be returned an object that represents the discovered brower device. This interface describes the various capabilities you may now introspect.

Additionally, the various device classes define algorithms for matching the devices they describe. By implementing this interface, you may provide additional logic around these capabilities.

Quick Start

The Zend_Http_UserAgent_Device interface defines the following methods.

  1. interface Zend_Http_UserAgent_Device extends Serializable
  2. {
  3.     public function __construct($userAgent = null, array $server = array(), array $config = array());
  4.     public static function match($userAgent, $server);
  5.     public function getAllFeatures();
  6.     public function getAllGroups();
  7.     public function getBrowser();
  8.     public function getBrowserVersion();
  9.     public function getGroup($group);
  10.     public function getImageFormatSupport();
  11.     public function getImages();
  12.     public function getMaxImageHeight();
  13.     public function getMaxImageWidth();
  14.     public function getPhysicalScreenHeight();
  15.     public function getPhysicalScreenWidth();
  16.     public function getPreferredMarkup();
  17.     public function getUserAgent();
  18.     public function getXhtmlSupportLevel();
  19.     public function hasFlashSupport();
  20.     public function hasPdfSupport();
  21.     public function hasPhoneNumber();
  22.     public function httpsSupport();
  23. }

The static function match() should be used to determine whether the provided User-Agent and environment (represented by the $server variable) match this device. If they do, the Zend_Http_UserAgent class will then create an instance of the class, passing it the User-Agent, $server array, and any configuration available; at this time, it is expected that the Device class will consult with a features adapter, if present, and populate itself with discovered capabilities.

In practice, you will likely extend Zend_Http_UserAgent_AbstractDevice, which provides functionality around discovering capabilities from the User-Agent string itself, as well as discovering and querying a Features adapter.

Configuration Options

Configuration options are defined on a per-device basis. The following options are defined in Zend_Http_UserAgent_AbstractDevice. Like all options, the "." character represents an additional level of depth to the configuration array.

Device Options

features.classname

The name of a Features adapter class that has either been previously loaded or which is discoverable via autoloading, or used in conjunction with the features.path option. This class must implement the Zend_Http_UserAgent_Features_Adapter interface.

features.path

If provided, the filesystem path to the features adapter class being used. The path must either be an absolute path or discoverable via the include_path.

Available Methods

__construct ( $userAgent = null, array $server = array(), array $config = array() )

Expects a User-Agent string, an array representing the HTTP environment, and an array of configuration values. The values are all optional, as they may be populated during deserialization.

match ( $userAgent, $server )

This method is static.

Provided the $userAgent string and an array representing the HTTP headers provided in the request, determine if they match the capabilities of this device. This method should return a boolean.

getAllFeatures

Returns an array of key/value pairs representing the features supported by this device instance.

getAllGroups

Similar to getAllFeatures(), this retrieves all features, but grouped by type.

hasFeature ( $feature )

Query the device to determine if it contains information on a specific feature.

getFeature ( $feature )

Retrieve the value of a specific device feature, if present. Typically, this will return a boolean false or a null value if the feature is not defined.

getBrowser

Returns the browser string as discoverd from the User-Agent string.

getBrowserVersion

Retrieve the browser version as discovered from the User-Agent string.

getGroup ( $group )

Get all features from a given feature group.

getImageFormatSupport

Retrieve a list of supported image types.

getImages

Alias for getImageFormatSupport().

getMaxImageHeight

Retrieve the maximum supported image height for the current device instance.

getMaxImageWidth

Retrieve the maximum supported image width for the current device instance.

getPhysicalScreenHeight

Retrieve the physical screen height for the current device instance.

getPhysicalScreenWidth

Retrieve the physical screen width for the current device instance.

getPreferredMarkup

Retrieve the preferred markup format for the current device instance.

getUserAgent

Retrieve the User-Agent string for the current device instance.

getXhtmlSupportLevel

Retrieve the supported X/HTML version for the current device instance.

hasFlashSupport

Determine if the current device instance supports Flash.

hasPdfSupport

Determine if the current device instance supports PDF.

hasPhoneNumber

Determine if the device has an associated phone number. Note: does not retrieve the phone number. This can be useful for determining if the device is a mobile phone versus another wireless capable device.

httpsSupport

Determine if the current device instance supports HTTPS.

Examples

Example #1 Determining Supported Features

You may wish to direct the user to specific areas of your site based on features supported by the device they are using. For instance, if a particular app works only in Flash, you might direct a non-Flash-capable device to a page indicating the application will not work on their device; or for a device not capable of HTTPS, you may indicate certain actions, such as purchases, are not available.

  1. $userAgent = new Zend_Http_UserAgent();
  2. $device    = $userAgent->getDevice();
  3.  
  4. // Redirect to a Flash version of the app:
  5. if ($device->hasFlashSupport()) {
  6.     header('Location: /flash/app');
  7.     exit;
  8. }
  9.  
  10. // Determine whether to show a "purchase" link:
  11. if ($device->httpsSupport()) {
  12.     echo '<a href="https://store-site.com/purchase">Purchase!</a>';
  13. } else {
  14.     echo 'Purchasing is unavailable on this device.';
  15. }

Example #2 Dynamically Scaling Images

You may wish to alter the image sizes present in order to achieve a specific design within mobile devices. You may use a variety of methods in the device object to make this happen.

  1. $userAgent = new Zend_Http_UserAgent();
  2. $device    = $userAgent->getDevice();
  3.  
  4. // Get the maximum image width and height
  5. $maxWidth  = $device->getMaxImageWidth();
  6. $maxHeight = $device->getMaxImageHeight();
  7.  
  8. // Create an <img> tag with appropriate sizes
  9. echo '<img src="/images/foo.png"';
  10. if ((null !== $maxWidth) && ($maxWidth < 328)) {
  11.     echo ' width="' . $maxWidth . '"';
  12. }
  13. if ((null !== $maxHeight) && ($maxHeight < 400)) {
  14.     echo ' height="' . $maxHeight . '"';
  15. }
  16. echo '/>';

Note: Markup- based scaling is not ideal
Markup-based scaling such as in the example above is not the best approach, as it means that the full-sized image is still delivered to the device. A better approach is to pre-scale your images to a variety of sizes representing the devices you wish to support, and then using the device capabilities to determine which image to use.
Another approach is to use third-party services. Zend Framework provides one such capability via the TinySrc view helper.

blog comments powered by Disqus