public function testCreateRequestForUserAgent() { $userAgent = 'testUA'; $header = array(Constants::HEADER_HTTP_USERAGENT => $userAgent); $expected = new GenericRequest($header, $userAgent, null, false); $result = $this->object->createRequestForUserAgent($userAgent); self::assertInstanceOf('\\Wurfl\\Request\\GenericRequest', $result); self::assertEquals($expected, $result); }
/** * @param \Wurfl\Device\ModelDeviceInterface[] $modelDevices Array of \Wurfl\Xml\ModelDevice objects * @param \Wurfl\Request\GenericRequest $request * * @throws \InvalidArgumentException if $modelDevices is not an array of at least one \Wurfl\Xml\ModelDevice */ public function __construct(array $modelDevices, Request\GenericRequest $request = null) { if (!is_array($modelDevices) || count($modelDevices) < 1) { throw new \InvalidArgumentException('modelDevices must be an array of at least one ModelDevice.'); } foreach ($modelDevices as $modelDevice) { if (!$modelDevice instanceof Device\ModelDeviceInterface) { throw new \InvalidArgumentException('each ModelDevice has to implement the \\Wurfl\\Device\\ModelDeviceInterface'); } $this->modelDevices[] = $modelDevice; } if ($request === null) { // This might happen if a device is looked up by its ID directly, without providing a user agent $requestFactory = new Request\GenericRequestFactory(); $request = $requestFactory->createRequestForUserAgent($this->userAgent); } $this->request = $request; }
/** * @param string|array|\Wurfl\Request\GenericRequest $request * * @return \Wurfl\Request\GenericRequest */ public function initRequest($request) { if (null === $request) { throw new \UnexpectedValueException('You have to call this function with the useragent parameter'); } $requestFactory = new GenericRequestFactory(); if (is_string($request)) { $request = $requestFactory->createRequestForUserAgent($request); } elseif (is_array($request)) { $request = $requestFactory->createRequest($request); } if (!$request instanceof GenericRequest) { throw new \UnexpectedValueException('the useragent parameter has to be a string, an array or an instance of \\Wurfl\\Request\\GenericRequest'); } return $request; }
/** * @dataProvider loadData * * @param string $expected * @param array $serverData */ public function testCreateRequest($expected, array $serverData) { $requestFactory = new GenericRequestFactory(); $request = $requestFactory->createRequest($serverData); self::assertEquals($expected, $request->getUserAgent()); }
/** * Wraps the model device with \Wurfl\Xml\ModelDeviceInterface. This function takes the * Device ID and returns the \Wurfl\CustomDevice with all capabilities. * * @param string $deviceId * @param Request\GenericRequest $request * * @return \Wurfl\CustomDevice */ private function getWrappedDevice($deviceId, Request\GenericRequest $request = null) { $modelDevices = $this->getCacheStorage()->load('DEVS_' . $deviceId); if (empty($modelDevices)) { $modelDevices = $this->getDeviceRepository()->getDeviceHierarchy($deviceId); } $this->getCacheStorage()->save('DEVS_' . $deviceId, $modelDevices); if ($request === null) { // If a request was not provided, we generate one from the WURFL entry itself // to help resolve the virtual capabilities $requestFactory = new GenericRequestFactory(); $request = $requestFactory->createRequestForUserAgent($modelDevices[0]->userAgent); $genericNormalizer = UserAgentHandlerChainFactory::createGenericNormalizers(); $request->setUserAgentNormalized($genericNormalizer->normalize($request->getUserAgent())); } // The CustomDevice is not cached since virtual capabilities must be recalculated // for every different request. return new CustomDevice($modelDevices, $request); }
/** * Returns a device for the given user-agent * * @param string $userAgent * * @throws Exception if $userAgent is not set * * @return \Wurfl\CustomDevice device */ public function getDeviceForUserAgent($userAgent = '') { if (!is_string($userAgent)) { $userAgent = ''; } $requestFactory = new Request\GenericRequestFactory(); $request = $requestFactory->createRequestForUserAgent($userAgent); $device = $this->getDeviceForRequest($request); return $device; }