|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Wikibase\DataModel\Statement; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use Traversable; |
| 7 | +use Wikibase\DataModel\Claim\Claims; |
| 8 | +use Wikibase\DataModel\Entity\PropertyId; |
| 9 | +use Wikibase\DataModel\Reference; |
| 10 | +use Wikibase\DataModel\ReferenceList; |
| 11 | +use Wikibase\DataModel\References; |
| 12 | +use Wikibase\DataModel\Snak\Snak; |
| 13 | +use Wikibase\DataModel\Snak\SnakList; |
| 14 | +use Wikibase\DataModel\Snak\Snaks; |
| 15 | + |
| 16 | +/** |
| 17 | + * Ordered, non-unique, collection of Statement objects. |
| 18 | + * Provides various filter operations though does not do any indexing by default. |
| 19 | + * |
| 20 | + * @since 1.0 |
| 21 | + * |
| 22 | + * @licence GNU GPL v2+ |
| 23 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 24 | + */ |
| 25 | +class StatementList implements \IteratorAggregate { |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Statement[] |
| 29 | + */ |
| 30 | + private $statements = array(); |
| 31 | + |
| 32 | + /** |
| 33 | + * @param Statement[]|Traversable $statements |
| 34 | + * |
| 35 | + * @throws InvalidArgumentException |
| 36 | + */ |
| 37 | + public function __construct( $statements = array() ) { |
| 38 | + $this->addStatements( $statements ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the best statements per property. |
| 43 | + * The best statements are those with the highest rank for a particular property. |
| 44 | + * Deprecated ranks are never included. |
| 45 | + * |
| 46 | + * @return self |
| 47 | + */ |
| 48 | + public function getBestStatementPerProperty() { |
| 49 | + $statementList = new self(); |
| 50 | + |
| 51 | + foreach ( $this->getPropertyIds() as $propertyId ) { |
| 52 | + $claims = new Claims( $this->statements ); |
| 53 | + $statementList->addStatements( $claims->getClaimsForProperty( $propertyId )->getBestClaims() ); |
| 54 | + } |
| 55 | + |
| 56 | + return $statementList; |
| 57 | + } |
| 58 | + |
| 59 | + private function addStatements( $statements ) { |
| 60 | + $this->assertAreStatements( $statements ); |
| 61 | + |
| 62 | + foreach ( $statements as $statement ) { |
| 63 | + $this->statements[] = $statement; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private function assertAreStatements( $statements ) { |
| 68 | + if ( !is_array( $statements ) && !( $statements instanceof Traversable ) ) { |
| 69 | + throw new InvalidArgumentException( '$statements should be an array or a Traversable' ); |
| 70 | + } |
| 71 | + |
| 72 | + foreach ( $statements as $statement ) { |
| 73 | + if ( !( $statement instanceof Statement ) ) { |
| 74 | + throw new InvalidArgumentException( 'All elements need to be of type Statement' ); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the property ids used by the statements. |
| 81 | + * The keys of the returned array hold the serializations of the property ids. |
| 82 | + * |
| 83 | + * @return PropertyId[] |
| 84 | + */ |
| 85 | + public function getPropertyIds() { |
| 86 | + $propertyIds = array(); |
| 87 | + |
| 88 | + foreach ( $this->statements as $statement ) { |
| 89 | + $propertyIds[$statement->getPropertyId()->getSerialization()] = $statement->getPropertyId(); |
| 90 | + } |
| 91 | + |
| 92 | + return $propertyIds; |
| 93 | + } |
| 94 | + |
| 95 | + public function addStatement( Statement $statement ) { |
| 96 | + $this->statements[] = $statement; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param Snak $mainSnak |
| 101 | + * @param Snak[]|Snaks|null $qualifiers |
| 102 | + * @param Reference[]|References|null $references |
| 103 | + * @param string|null $guid |
| 104 | + */ |
| 105 | + public function addNewStatement( Snak $mainSnak, $qualifiers = null, $references = null, $guid = null ) { |
| 106 | + $qualifiers = is_array( $qualifiers ) ? new SnakList( $qualifiers ) : $qualifiers; |
| 107 | + $references = is_array( $references ) ? new ReferenceList( $references ) : $references; |
| 108 | + |
| 109 | + $statement = new Statement( $mainSnak, $qualifiers, $references ); |
| 110 | + $statement->setGuid( $guid ); |
| 111 | + |
| 112 | + $this->addStatement( $statement ); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Statements that have a main snak already in the list are filtered out. |
| 117 | + * The last occurrences are retained. |
| 118 | + * |
| 119 | + * @return self |
| 120 | + */ |
| 121 | + public function getWithUniqueMainSnaks() { |
| 122 | + $statements = array(); |
| 123 | + |
| 124 | + foreach ( $this->statements as $statement ) { |
| 125 | + $statements[$statement->getMainSnak()->getHash()] = $statement; |
| 126 | + } |
| 127 | + |
| 128 | + return new self( $statements ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @return Traversable |
| 133 | + */ |
| 134 | + public function getIterator() { |
| 135 | + return new \ArrayIterator( $this->statements ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return Statement[] |
| 140 | + */ |
| 141 | + public function toArray() { |
| 142 | + return $this->statements; |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments