/** * Tests Text::userAgent * @test */ public function test_user_agent_accepts_array() { $agent_info = Text::userAgent('Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 ' . '(KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36', array('browser', 'version', 'platform')); $this->assertArrayHasKey('browser', $agent_info); $this->assertArrayHasKey('version', $agent_info); $this->assertArrayHasKey('platform', $agent_info); }
/** * Returns information about the client user agent. * * // Returns "Chrome" when using Google Chrome * $browser = Text::userAgent($agent, 'browser'); * * Multiple values can be returned at once by using an array: * * // Get the browser and platform with a single call * $info = Text::userAgent($agent, array('browser', 'platform')); * * When using an array for the value, an associative array will be returned. * * @param string $agent userAgent * @param mixed $value array or string to return: browser, version, robot, mobile, platform * @return mixed requested information, false if nothing is found * @uses Kohana::$config */ public static function userAgent($agent, $value) { if (is_array($value)) { $data = array(); foreach ($value as $part) { // Add each part to the set $data[$part] = Text::userAgent($agent, $part); } return $data; } if ($value === 'browser' || $value == 'version') { // Extra data will be captured $info = array(); // Load browsers $browsers = \Phalcana\Phalcana::$di->get('config')->load('user_agents')->browser; foreach ($browsers as $search => $name) { if (stripos($agent, $search) !== false) { // Set the browser name $info['browser'] = $name; if (preg_match('#' . preg_quote($search) . '[^0-9.]*+([0-9.][0-9.a-z]*)#i', $agent, $matches)) { // Set the version number $info['version'] = $matches[1]; } else { // No version number found $info['version'] = false; } return $info[$value]; } } } else { // Load the search group for this type $group = \Phalcana\Phalcana::$di->get('config')->load('user_agents')->{$value}; foreach ($group as $search => $name) { if (stripos($agent, $search) !== false) { // Set the value name return $name; } } } // The value requested could not be found return false; }