<?php

require_once 'Zend/View/Helper/Abstract.php';

/**
 * View Helper to create a string with browser information from Zend_Http_UserAgent
 */
class Fiktiv_View_Helper_BrowserString extends Zend_View_Helper_Abstract
{
    /**
     * Variable to hold the browser string.
     *
     * @var string
     */
    private static $_info = "";

    /**
     * Helper method to format the version part of the string.
     *
     * @param  Zend_Http_UserAgent $httpAgent
     * @return string
     */
    private function _getVersion(Zend_Http_UserAgent $httpAgent)
    {
        // Trim the version down abit.
        $parts = explode('.', $httpAgent->getDevice()->getBrowserVersion());
        return implode('.', array_slice($parts, 0, 2));
    }
    
    /**
     * Constructor
     *
     * Constructs and stores the browser string to 
     * later be retrieved by the helper method.
     *
     * @return void
     */
    public function __construct()
    {
        // Construct the string.
        $httpAgent = new Zend_Http_UserAgent();
        
        $this->_info = $httpAgent->getDevice()->getBrowser() . " "
                    . $this->_getVersion($httpAgent);
    }

    /**
     * The Actual helper method.
     *
     * @return string
     */
    public function browserString()
    {
        return $this->_info;
    }
}
