web/lib/Zend/Http/UserAgent/Features/Adapter/TeraWurfl.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Http
       
    17  * @subpackage UserAgent
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 /**
       
    23  * Zend_Http_UserAgent_Features_Adapter_Interface
       
    24  */
       
    25 require_once 'Zend/Http/UserAgent/Features/Adapter.php';
       
    26 
       
    27 /**
       
    28  * Features adapter build with the Tera Wurfl Api
       
    29  * See installation instruction here : http://www.tera-wurfl.com/wiki/index.php/Installation 
       
    30  * Download : http://www.tera-wurfl.com/wiki/index.php/Downloads
       
    31  *
       
    32  * @package    Zend_Http
       
    33  * @subpackage UserAgent
       
    34  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    35  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    36  */
       
    37 class Zend_Http_UserAgent_Features_Adapter_TeraWurfl implements Zend_Http_UserAgent_Features_Adapter
       
    38 {
       
    39     /**
       
    40      * Get features from request
       
    41      *
       
    42      * @param  array $request $_SERVER variable
       
    43      * @return array
       
    44      */
       
    45     public static function getFromRequest($request, array $config)
       
    46     {
       
    47         if (!class_exists('TeraWurfl')) {
       
    48             // If TeraWurfl class not found, see if we can load it from 
       
    49             // configuration
       
    50             //
       
    51             if (!isset($config['terawurfl'])) {
       
    52                 // No configuration
       
    53                 require_once 'Zend/Http/UserAgent/Features/Exception.php';
       
    54                 throw new Zend_Http_UserAgent_Features_Exception('"TeraWurfl" configuration is not defined');
       
    55             }
       
    56             
       
    57             $config = $config['terawurfl'];
       
    58 
       
    59              if (empty($config['terawurfl_lib_dir'])) {
       
    60                 // No lib_dir given
       
    61                 require_once 'Zend/Http/UserAgent/Features/Exception.php';
       
    62                 throw new Zend_Http_UserAgent_Features_Exception('The "terawurfl_lib_dir" parameter is not defined');
       
    63             }
       
    64 
       
    65             // Include the Tera-WURFL file
       
    66             require_once ($config['terawurfl_lib_dir'] . '/TeraWurfl.php');
       
    67         }
       
    68         
       
    69         
       
    70         // instantiate the Tera-WURFL object
       
    71         $wurflObj = new TeraWurfl();
       
    72         
       
    73         // Get the capabilities of the current client.
       
    74         $matched = $wurflObj->getDeviceCapabilitiesFromRequest(array_change_key_case($request, CASE_UPPER));
       
    75         
       
    76         return self::getAllCapabilities($wurflObj);
       
    77     }
       
    78 
       
    79     /***
       
    80      * Builds an array with all capabilities
       
    81      * 
       
    82      * @param TeraWurfl $wurflObj TeraWurfl object
       
    83      */
       
    84     public static function getAllCapabilities(TeraWurfl $wurflObj)
       
    85     {
       
    86         
       
    87         foreach ($wurflObj->capabilities as $group) {
       
    88             if (!is_array($group)) {
       
    89                 continue;
       
    90             }
       
    91             while (list ($key, $value) = each($group)) {
       
    92                 if (is_bool($value)) {
       
    93                     // to have the same type than the official WURFL API
       
    94                     $features[$key] = ($value ? 'true' : 'false');
       
    95                 } else {
       
    96                     $features[$key] = $value;
       
    97                 }
       
    98             }
       
    99         }
       
   100         return $features;
       
   101     }
       
   102 }