/** * @dataProvider precisionDetectionProvider * @param string $value * @param float|int $expected */ public function testPrecisionDetection($value, $expected) { $parser = new GlobeCoordinateParser(); /** @var GlobeCoordinateValue $globeCoordinateValue */ $globeCoordinateValue = $parser->parse($value); $this->assertSame($expected, $globeCoordinateValue->getPrecision()); }
/** * @dataProvider validProvider */ public function testFormatterRoundTrip(GlobeCoordinateValue $coord, $expectedValue, FormatterOptions $options) { $formatter = new GlobeCoordinateFormatter($options); $parser = new GlobeCoordinateParser(new ParserOptions(array('precision' => $coord->getPrecision()))); $formatted = $formatter->format($coord); $parsed = $parser->parse($formatted); // NOTE: $parsed may be != $coord, because of rounding, so we can't compare directly. $formattedParsed = $formatter->format($parsed); $this->assertEquals($formatted, $formattedParsed); }
/** * @param string $serialization * @return DataValue */ private function parseDataValue($serialization) { if (preg_match('/^Q\\d+$/', $serialization)) { return new EntityIdValue(new ItemId($serialization)); } elseif (preg_match('/^P\\d+$/', $serialization)) { return new EntityIdValue(new PropertyId($serialization)); } elseif (preg_match('/^@([+\\-]?\\d+(\\.\\d+)?)\\/([+\\-]?\\d+(\\.\\d+)?)$/', $serialization, $m)) { $globeCoordinateParser = new GlobeCoordinateParser(); return $globeCoordinateParser->parse($m[1] . ' ' . $m[2]); //Hacky but works: make the parser guess the precision } elseif (preg_match('/^([+-]\\d+-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\dZ)\\/(\\d+)$/', $serialization, $m)) { return new TimeValue($m[1], 0, 0, 0, intval($m[2]), 'http://www.wikidata.org/entity/Q1985727'); } elseif (preg_match('/^[+-]\\d+(\\.\\d+)?$/', $serialization, $m)) { $decimalValue = new DecimalValue($serialization); return new QuantityValue($decimalValue, '1', $decimalValue, $decimalValue); } elseif (preg_match('/^(\\w+):"(.*)"$/', $serialization, $m)) { return new MonolingualTextValue($m[1], $m[2]); } elseif (preg_match('/^"(.*)"$/', $serialization, $m)) { return new StringValue($m[1]); } else { throw new InvalidArgumentException("Unknown DataValue serialization: {$serialization}"); //TODO: implement the other types of statements } }