ZF-10696: TeraWurfl webservice adapter

Description

<?php /** * @version $Revision: 30 $ * @category Zend * @package Zend_Http_UserAgent * @subpackage Features_Adapter * @author $Author: andrew.stephanoff@gmail.com $ $Date: 2010-11-12 22:59:51 +0100 (Пт, 12 ноя 2010) $ */

/** * Tera-WURFL webservice adapter for {@link Zend_Http_UserAgent}. */ class Zend_Http_UserAgent_Features_Adapter_TeraWurflWebservice implements Zend_Http_UserAgent_Features_Adapter { const EXC_USER_AGENT_NOT_DEFINED = '"HTTP_USER_AGENT" not defined in request variable';

const EXC_URL_PARAMETER_NOT_DEFINED =
    'The "terawurfl_webservice_url" configuration parameter is not defined';

const EXC_CURL_ERROR = 'CURL error: %d - %s';

const EXC_JSON_DECODE_ERROR = 'Mailformed JSON string: %s';

/**
 * Get features from request.
 *
 * @param array $request $_SERVER variable
 * @param array $config configuration array
 * @return array list of features
 */
public static function getFromRequest($request, array $config)
{
    list($url, $userAgent) = self::_getUrlAndUserAgent($request, $config);

    $json = self::_doRequest($url, $userAgent);
    $data = self::_processResponse($json);

    return $data['capabilities'];
}

/**
 * Return list with URL and UserAgent variables.
 *
 * @param array $request $_SERVER variable
 * @param array $config configuration array
 * @return array $url and $userAgent values
 * @throws Zend_Http_UserAgent_Features_Exception
 */
protected static function _getUrlAndUserAgent($request, array $config)
{
    // check HTTP_USER_AGENT availability
    if (isset($request['http_user_agent'])) {
        $userAgent = $request['http_user_agent'];

    } else {
        throw new Zend_Http_UserAgent_Features_Exception(
            self::EXC_USER_AGENT_NOT_DEFINED);
    }

    // check URL availability
    if (isset($config['terawurfl_webservice_url'])) {
        $url = $config['terawurfl_webservice_url'];

    } else {
        throw new Zend_Http_UserAgent_Features_Exception(
            self::EXC_URL_PARAMETER_NOT_DEFINED);
    }
    return array($url, $userAgent);
}

/**
 * Request to webservice
 *
 * @param string $url
 * @param string $userAgent
 * @return string JSON encoded data
 * @throws Zend_Http_UserAgent_Features_Exception
 */
protected static function _doRequest($url, $userAgent)
{
    // build URL
    if (false === strpos($url, 'http')) {
        $url = 'http://' . $url;
    }
    if (false === strpos($url, 'webservice.php')) {
        if ('/' != substr($url, strlen($url) - 1)) {
            $url .= '/';
        }
        $url .= 'webservice.php';
    }
    $args = http_build_query(array(
        'ua' => $userAgent,
        'format' => 'json',
    ));
    $url .= '?' . $args;

    // read data from webservice
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
    ));
    $response = curl_exec($ch);

    // validate for errors
    if (0 == ($errno = curl_errno($ch))) {
        curl_close($ch);
        return $response;

    } else {
        $message = sprintf(self::EXC_CURL_ERROR, $errno, curl_error($ch));
        curl_close($ch);
        throw new Zend_Http_UserAgent_Features_Exception($message);
    }
}

/**
 * Return response object
 *
 * @param string $json
 * @return stdClass Tera-WURFL capabilities object
 * @throws Zend_Http_UserAgent_Features_Exception
 * @todo validate for errors
 */
protected  static function _processResponse($json)
{
    if (null === ($data = json_decode($json, true))) {
        $message = sprintf(self::EXC_JSON_DECODE_ERROR, $json);
        throw new Zend_Http_UserAgent_Features_Exception($message);
    }
    return $data;
}

}

Comments

No comments to display