Browscap\Formatter\JsonFormatter::formatPropertyValue PHP Method

formatPropertyValue() public method

formats the name of a property
public formatPropertyValue ( string $value, string $property ) : string
$value string
$property string
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;
    }

Usage Example

 /**
  * tests formatting a property value
  *
  * @dataProvider propertyNameTypeDataProvider
  *
  * @param string $propertyName
  * @param string $inputValue
  * @param string $expectedValue
  *
  * @group formatter
  * @group sourcetest
  */
 public function testFormatPropertyValue($propertyName, $inputValue, $expectedValue)
 {
     $actualValue = $this->object->formatPropertyValue($inputValue, $propertyName);
     self::assertSame($expectedValue, $actualValue, "Property {$propertyName} should be {$expectedValue} (was {$actualValue})");
 }