/**
  * @dataProvider provideParse
  */
 public function testParse($input, StringNormalizer $normalizer = null, DataValue $expected)
 {
     $parser = new StringParser($normalizer);
     $value = $parser->parse($input);
     $this->assertInstanceOf('DataValues\\StringValue', $value);
     $this->assertEquals($expected->toArray(), $value->toArray());
 }
 /**
  * @dataProvider instanceProvider
  * @param DataValue $value
  * @param array $arguments
  */
 public function testToArray(DataValue $value, array $arguments)
 {
     $array = $value->toArray();
     $this->assertInternalType('array', $array);
     $this->assertTrue(array_key_exists('type', $array));
     $this->assertTrue(array_key_exists('value', $array));
     $this->assertEquals($value->getTargetType(), $array['type']);
     $this->assertEquals($value->getValue(), $array['value']);
 }
 /**
  * @see ValueValidator::validate()
  *
  * @param DataValue $value The value to validate
  *
  * @throws InvalidArgumentException
  * @return Result
  */
 public function validate($value)
 {
     if (!$value instanceof DataValue) {
         throw new InvalidArgumentException('DataValue expected');
     }
     $arrayValue = $value->getArrayValue();
     $result = $this->validator->validate($arrayValue);
     return $result;
 }
 /**
  * @dataProvider provideApiRequest
  */
 public function testApiRequest(DataValue $value, $dataType, $format, $options, $pattern)
 {
     $this->setUpEntities();
     $params = array('action' => 'wbformatvalue', 'generate' => $format, 'datatype' => $dataType, 'datavalue' => json_encode($value->toArray()), 'options' => $options === null ? null : json_encode($options));
     list($resultArray, ) = $this->doApiRequest($params);
     $this->assertInternalType('array', $resultArray, 'top level element must be an array');
     $this->assertArrayHasKey('result', $resultArray, 'top level element must have a "result" key');
     $this->assertRegExp($pattern, $resultArray['result']);
 }
 /**
  * @see DataValueChecker::checkDataValue
  *
  * @param DataValue $dataValue
  * @return boolean
  */
 public function checkDataValue(DataValue $dataValue)
 {
     foreach ($this->values as $value) {
         if ($dataValue->equals($value)) {
             return true;
         }
     }
     return false;
 }
 /**
  * Creates a value node for $value, and attaches it to the current subject of $writer.
  * If a value node for $value was already created, null is returned. Otherwise, the
  * value node's lname is returned, which should be used to generate detailed about the
  * value into the writer returned by getValueNodeWriter().
  *
  * When this method returns a non-null lname, the current subject of the RdfWriter returned by
  * getValueNodeWriter() will the be value node with that lname.
  *
  * @param RdfWriter $writer
  * @param string $propertyValueNamespace Property value relation namespace
  * @param string $propertyValueLName Property value relation name
  * @param string $dataType Property data type (unused, passed here for symmetry
  *        with the signature of ValueSnakRdfBuilder::addValue).
  * @param DataValue $value
  *
  * @return string|null The LName of the value node (in the RdfVocabulary::NS_VALUE namespace),
  *  or null if the value node should not be processed (generally, because it already has
  *  been processed).
  */
 public function attachValueNode(RdfWriter $writer, $propertyValueNamespace, $propertyValueLName, $dataType, DataValue $value)
 {
     $valueLName = $value->getHash();
     $writer->say(RdfVocabulary::$claimToValue[$propertyValueNamespace], $propertyValueLName)->is(RdfVocabulary::NS_VALUE, $valueLName);
     if ($this->dedupeBag->alreadySeen($valueLName, 'V') !== false) {
         return null;
     }
     $this->valueNodeWriter->about(RdfVocabulary::NS_VALUE, $valueLName)->a(RdfVocabulary::NS_ONTOLOGY, $this->vocabulary->getValueTypeName($value));
     return $valueLName;
 }
 /**
  * @see Comparable::equals
  *
  * @param DiffRangeConstraint $constraint
  * @return boolean
  */
 public function equals($constraint)
 {
     if ($constraint === $this) {
         return true;
     }
     if (!$constraint instanceof self) {
         return false;
     }
     return $this->diff->equals($constraint->diff);
 }
 /**
  * @param DataValue|mixed $expected
  * @param DataValue|mixed $actual
  */
 private function assertSmartEquals($expected, $actual)
 {
     if ($this->requireDataValue() || $expected instanceof Comparable) {
         if ($expected instanceof DataValue && $actual instanceof DataValue) {
             $msg = "testing equals():\n" . preg_replace('/\\s+/', ' ', print_r($actual->toArray(), true)) . " should equal\n" . preg_replace('/\\s+/', ' ', print_r($expected->toArray(), true));
         } else {
             $msg = 'testing equals()';
         }
         $this->assertTrue($expected->equals($actual), $msg);
     } else {
         $this->assertEquals($expected, $actual);
     }
 }
Beispiel #9
0
 /**
  * @see Hashable::getHash
  *
  * @since 1.0
  *
  * @return string
  */
 public function getHash()
 {
     if ($this->hash === null) {
         $this->hash = sha1($this->getType() . $this->propertyId->getHash() . $this->subDescription->getHash() . $this->isSubProperty);
     }
     return $this->hash;
 }
 /**
  * @see Comparable::equals
  *
  * @param RangeChecker $constraint
  * @return boolean
  */
 public function equals($constraint)
 {
     if ($constraint === $this) {
         return true;
     }
     if (!$constraint instanceof self) {
         return false;
     }
     return $this->minValue->equals($constraint->minValue) && $this->maxValue->equals($constraint->maxValue);
 }
 /**
  * @param DataValue $dataValue
  */
 private function processDataValue(DataValue $dataValue)
 {
     if ($dataValue instanceof EntityIdValue) {
         $entityId = $dataValue->getEntityId();
         $this->entityIds[$entityId->getSerialization()] = $entityId;
     } elseif ($dataValue instanceof QuantityValue) {
         $unitUri = $dataValue->getUnit();
         $this->processUri($unitUri);
     }
     // TODO: EntityIds from GlobeCoordinateValue's globe URI (Wikidata, not local item URI!)
     // TODO: EntityIds from TimeValue's calendar URI (Wikidata, not local item URI!)
 }
 /**
  * @see Hashable::getHash
  *
  * @since 1.0
  *
  * @return string
  */
 public function getHash()
 {
     return sha1($this->getType() . $this->value->getHash() . $this->comparator);
 }
 /**
  * Get Wikibase value type name for ontology
  *
  * @param DataValue $val
  *
  * @return string
  */
 public function getValueTypeName(DataValue $val)
 {
     return ucfirst($val->getType()) . 'Value';
 }
 /**
  * @see ValueFormatter::format
  *
  * Formats the given value by finding an appropriate formatter among the ones supplied
  * to the constructor, and applying it.
  *
  * If $dataTypeId is given, this will first try to find an appropriate formatter based on
  * the data type. If none is found, this falls back to finding a formatter based on the
  * value's type.
  *
  * @see TypedValueFormatter::formatValue
  *
  * @param DataValue $value
  * @param string|null $dataTypeId
  *
  * @throws FormattingException
  * @return string
  */
 public function formatValue(DataValue $value, $dataTypeId = null)
 {
     $formatter = $this->getFormatter($value->getType(), $dataTypeId);
     $text = $formatter->format($value);
     return $text;
 }
Beispiel #15
0
 /**
  * @see Copyable::getCopy
  *
  * @return Snak
  */
 public function getCopy()
 {
     $value = $this->dataValue !== null ? $this->dataValue->getCopy() : null;
     return new self($this->type, $this->propertyId, $value);
 }
 protected function getSerializedDataValue(DataValue $dataValue)
 {
     return $dataValue->toArray();
 }
 /**
  * @param DataValue $value
  *
  * @return boolean
  */
 private function isUnDeserializableValue(DataValue $value)
 {
     return $value->getType() === UnDeserializableValue::getType();
 }
 /**
  * @param DataValue $value
  *
  * @return string the object URI
  */
 protected function getValueUri(DataValue $value)
 {
     return trim($value->getValue());
 }
 /**
  * Validates the given data value using the given data type.
  *
  * @param DataValue $dataValue
  * @param string    $dataTypeId
  *
  * @return Result
  */
 public function validateDataValue(DataValue $dataValue, $dataTypeId)
 {
     $dataValueType = $this->dataTypeFactory->getType($dataTypeId)->getDataValueType();
     if ($dataValue instanceof UnDeserializableValue) {
         $result = Result::newError(array(Error::newError('Bad snak value: ' . $dataValue->getReason(), null, 'bad-value', array($dataValue->getReason()))));
     } elseif ($dataValueType != $dataValue->getType()) {
         $result = Result::newError(array(Error::newError('Bad value type: ' . $dataValue->getType() . ', expected ' . $dataValueType, null, 'bad-value-type', array($dataValue->getType(), $dataValueType))));
     } else {
         $result = Result::newSuccess();
     }
     //XXX: DataTypeValidatorFactory should expose only one validator, which would be a CompositeValidator
     foreach ($this->validatorFactory->getValidators($dataTypeId) as $validator) {
         $subResult = $validator->validate($dataValue);
         //XXX: Some validators should be fatal and cause us to abort the loop.
         //     Others shouldn't.
         if (!$subResult->isValid()) {
             //TODO: Don't bail out immediately. Accumulate errors from all validators.
             //      We need Result::merge() for this.
             return $subResult;
         }
     }
     return $result;
 }
 public function formatValueAsText(DataValue $value)
 {
     return print_r($value->getValue(), true);
 }
 /**
  * @param DataValue $value
  *
  * @return string
  */
 protected function getLiteralValue(DataValue $value)
 {
     return $value->getValue();
 }
 /**
  * @param DataValue $value
  *
  * @return string the object URI
  */
 protected function getValueUri(DataValue $value)
 {
     return $this->vocabulary->getCommonsURI($value->getValue());
 }
 /**
  * @see Hashable::getHash
  *
  * @since 1.0
  *
  * @return string
  */
 public function getHash()
 {
     return sha1($this->getType() . $this->propertyId->getHash());
 }