/** * formats the name of a property * * @param string $value * @param string $property * * @return string */ public function formatPropertyValue($value, $property) { $valueOutput = $value; $propertyHolder = new PropertyHolder(); switch ($propertyHolder->getPropertyType($property)) { case PropertyHolder::TYPE_BOOLEAN: if (true === $value || $value === 'true') { $valueOutput = 'true'; } elseif (false === $value || $value === 'false') { $valueOutput = 'false'; } else { $valueOutput = ''; } break; case PropertyHolder::TYPE_IN_ARRAY: try { $valueOutput = $propertyHolder->checkValueInArray($property, $value); } catch (\InvalidArgumentException $ex) { $valueOutput = ''; } break; default: // nothing t do here break; } if ('unknown' === $valueOutput) { $valueOutput = ''; } return '"' . str_replace('"', '""', $valueOutput) . '"'; }
/** * formats the name of a property * * @param string $value * @param string $property * * @return string */ public function formatPropertyValue($value, $property) { $propertyHolder = new PropertyHolder(); switch ($propertyHolder->getPropertyType($property)) { case PropertyHolder::TYPE_STRING: $valueOutput = json_encode(trim($value)); break; case PropertyHolder::TYPE_BOOLEAN: if (true === $value || $value === 'true') { $valueOutput = 'true'; } elseif (false === $value || $value === 'false') { $valueOutput = 'false'; } else { $valueOutput = '""'; } break; case PropertyHolder::TYPE_IN_ARRAY: try { $valueOutput = json_encode($propertyHolder->checkValueInArray($property, $value)); } catch (\InvalidArgumentException $ex) { $valueOutput = '""'; } break; default: $valueOutput = json_encode($value); break; } if ('unknown' === $valueOutput || '"unknown"' === $valueOutput) { $valueOutput = '""'; } return $valueOutput; }
/** * formats the name of a property * * @param string $value * @param string $property * * @return string */ public function formatPropertyValue($value, $property) { $valueOutput = $value; $propertyHolder = new PropertyHolder(); switch ($propertyHolder->getPropertyType($property)) { case PropertyHolder::TYPE_STRING: $valueOutput = '"' . trim($value) . '"'; break; case PropertyHolder::TYPE_BOOLEAN: if (true === $value || $value === 'true') { $valueOutput = '"true"'; } elseif (false === $value || $value === 'false') { $valueOutput = '"false"'; } else { $valueOutput = ''; } break; case PropertyHolder::TYPE_IN_ARRAY: try { $valueOutput = '"' . $propertyHolder->checkValueInArray($property, $value) . '"'; } catch (\InvalidArgumentException $ex) { $valueOutput = ''; } break; default: if (preg_match('/[^a-zA-Z0-9]/', $valueOutput)) { $valueOutput = '"' . $valueOutput . '"'; } // nothing t do here break; } return $valueOutput; }
/** * checks if a property should be in the output * * @param string $property * @param \Browscap\Writer\WriterInterface|null $writer * * @return bool */ public function isOutputProperty($property, WriterInterface $writer = null) { $propertyHolder = new PropertyHolder(); if (!$propertyHolder->isOutputProperty($property, $writer)) { return false; } return $propertyHolder->isLiteModeProperty($property); }
/** * checks if a property should be in the output * * @param string $property * @param \Browscap\Writer\WriterInterface|null $writer * * @return boolean */ public function isOutputProperty($property, WriterInterface $writer = null) { $propertyHolder = new PropertyHolder(); if (!$propertyHolder->isOutputProperty($property, $writer)) { return false; } return in_array($property, $this->fields); }
/** * @dataProvider userAgentDataProvider * @coversNothing * * @param string $userAgent * @param array $expectedProperties * @param bool $lite * * @group integration * @group useragenttest * @group lite */ public function testUserAgentsLite($userAgent, $expectedProperties, $lite = true) { if (!is_array($expectedProperties) || !count($expectedProperties)) { self::markTestSkipped('Could not run test - no properties were defined to test'); } if (!$lite) { self::markTestSkipped('Test skipped - Browser/Platform/Version not defined for Lite Mode'); } self::$browscap->iniFilename = 'lite_browscap.ini'; self::$browscap->localFile = self::$buildFolder . '/lite_php_browscap.ini'; self::$browscap->cacheFilename = 'cache-lite.php'; self::$browscap->doAutoUpdate = false; self::$browscap->silent = false; self::$browscap->updateMethod = Browscap::UPDATE_LOCAL; static $updatedLiteCache = false; if (!$updatedLiteCache) { self::$browscap->updateCache(); $updatedLiteCache = true; } $actualProps = (array) self::$browscap->getBrowser($userAgent); foreach ($expectedProperties as $propName => $propValue) { if (!self::$propertyHolder->isOutputProperty($propName)) { continue; } if (!self::$propertyHolder->isLiteModeProperty($propName)) { continue; } self::assertArrayHasKey($propName, $actualProps, 'Actual properties did not have "' . $propName . '" property'); self::assertSame($propValue, $actualProps[$propName], 'Expected actual "' . $propName . '" to be "' . $propValue . '" (was "' . $actualProps[$propName] . '"; used pattern: ' . $actualProps['browser_name_pattern'] . ')'); } }
/** * @dataProvider userAgentDataProvider * @coversNothing * * @param string $userAgent * @param array $expectedProperties * @param bool $lite * @param bool $standard * * @throws \Exception * @throws \BrowscapPHP\Exception * * @group intergration * @group useragenttest * @group lite */ public function testUserAgentsLite($userAgent, $expectedProperties, $lite = true, $standard = true) { if (!is_array($expectedProperties) || !count($expectedProperties)) { self::markTestSkipped('Could not run test - no properties were defined to test'); } if (!$lite) { self::markTestSkipped('Test skipped - Browser/Platform/Version not defined for Lite Mode'); } static $updatedLiteCache = false; if (!$updatedLiteCache) { self::$browscap->getCache()->flush(); self::$browscap->convertFile(self::$buildFolder . '/lite_php_browscap.ini'); $updatedLiteCache = true; } $actualProps = (array) self::$browscap->getBrowser($userAgent); foreach ($expectedProperties as $propName => $propValue) { if (!self::$propertyHolder->isOutputProperty($propName)) { continue; } if (!self::$propertyHolder->isLiteModeProperty($propName)) { continue; } $propName = strtolower($propName); self::assertArrayHasKey($propName, $actualProps, 'Actual properties did not have "' . $propName . '" property'); self::assertSame($propValue, $actualProps[$propName], 'Expected actual "' . $propName . '" to be "' . $propValue . '" (was "' . $actualProps[$propName] . '"; used pattern: ' . $actualProps['browser_name_pattern'] . ')'); } }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage invalid value given for Property "Browser_Type": given value "bcd", allowed: ["Useragent Anonymizer","Browser","Offline Browser","Multimedia Player","Library","Feed Reader","Email Client","Bot\/Crawler","Application","Tool","unknown"] * * @group data * @group sourcetest */ public function testCheckValueInArrayExceptionWrongValue() { $this->object->checkValueInArray('Browser_Type', 'bcd'); }
/** * checks if a property should be in the output * * @param string $property * @param \Browscap\Writer\WriterInterface|null $writer * * @return bool */ public function isOutputProperty($property, WriterInterface $writer = null) { $propertyHolder = new PropertyHolder(); return $propertyHolder->isOutputProperty($property, $writer); }