/** * Adds Statements to the RDF graph. * * @param EntityId $entityId * @param StatementList $statementList */ public function addStatements(EntityId $entityId, StatementList $statementList) { // FIXME: getBestStatementPerProperty() uis expensive, share the result with FullStatementRdfBuilder! foreach ($statementList->getPropertyIds() as $propertyId) { foreach ($statementList->getByPropertyId($propertyId)->getBestStatements() as $statement) { $this->addMainSnak($entityId, $statement); } } }
/** * @param StatementList $statements * @param PropertyId $propertyId * @return DataValue[] */ public function getBestValues(StatementList $statements, PropertyId $propertyId) { $values = array(); $bestStatements = $statements->getByPropertyId($propertyId)->getBestStatements(); foreach ($bestStatements->toArray() as $statement) { $mainSnak = $statement->getMainSnak(); if ($mainSnak instanceof PropertyValueSnak) { $values[] = $mainSnak->getDataValue(); } } return $values; }
/** * @param StatementList $statements * @param PropertyId $propertyId * * @return DataValue[] */ private function getStatementValuesWithPropertyId(StatementList $statements, PropertyId $propertyId) { $statementValues = []; /** * @var Statement $statement */ foreach ($statements->getByPropertyId($propertyId)->getBestStatements() as $statement) { $snak = $statement->getMainSnak(); if ($snak instanceof PropertyValueSnak) { $statementValues[] = $this->handle($snak); } } return $statementValues; }
/** * @param StatementList $statements * * @return array */ public function buildFromStatements(StatementList $statements) : array { $simpleStatements = []; foreach ($statements->getPropertyIds() as $propertyId) { $statementValues = $this->getStatementMainValues($statements->getByPropertyId($propertyId)->getBestStatements()); if (!empty($statementValues)) { $simpleStatement = new SimpleStatement(); $simpleStatement->values = $statementValues; $simpleStatement->valueType = $statementValues[0]->getType(); $simpleStatement->propertyName = $this->getEntityName($propertyId); $simpleStatement->propertyId = $propertyId; $simpleStatement->propertyUrl = $this->urlBuilder->getApiPropertyUrl($propertyId); $simpleStatements[] = $simpleStatement; } } return $simpleStatements; }
/** * @param StatementList $statements * * @return string|null The string value of the main snak of the first best * "formatterUrlProperty" statements, if such exists. Null otherwise. */ private function getFormatterUrl(StatementList $statements) { if ($this->formatterUrlProperty === null) { return null; } $bestStatements = $statements->getByPropertyId($this->formatterUrlProperty)->getBestStatements(); if ($bestStatements->isEmpty()) { return null; } $statementArray = $bestStatements->toArray(); $mainSnak = $statementArray[0]->getMainSnak(); if (!$mainSnak instanceof PropertyValueSnak) { return null; } $dataValue = $mainSnak->getDataValue(); if (!$dataValue instanceof StringValue) { return null; } return $dataValue->getValue(); }
public function testGivenKnownPropertyId_getByPropertyIdReturnsListWithOnlyMatchingStatements() { $list = new StatementList(); $list->addNewStatement(new PropertyNoValueSnak(42)); $list->addNewStatement(new PropertyNoValueSnak(9001)); $list->addNewStatement(new PropertySomeValueSnak(42)); $list->addNewStatement(new PropertySomeValueSnak(9001)); $expected = new StatementList(); $expected->addNewStatement(new PropertyNoValueSnak(42)); $expected->addNewStatement(new PropertySomeValueSnak(42)); $this->assertEquals($expected, $list->getByPropertyId(new PropertyId('P42'))); }
/** * Adds Statements to the RDF graph. * * @param EntityId $entityId * @param StatementList $statementList */ public function addStatements(EntityId $entityId, StatementList $statementList) { $bestList = array(); // FIXME: This is expensive, share the result with TruthyStatementRdfBuilder! foreach ($statementList->getPropertyIds() as $propertyId) { $bestStatements = $statementList->getByPropertyId($propertyId)->getBestStatements(); foreach ($bestStatements->toArray() as $statement) { $bestList[$statement->getGuid()] = true; } } foreach ($statementList->toArray() as $statement) { $this->addStatement($entityId, $statement, isset($bestList[$statement->getGuid()])); } }