repo/includes/ChangeOp/ChangeOpRemoveStatement.php
<?php
namespace Wikibase\Repo\ChangeOp;
use InvalidArgumentException;
use ValueValidators\Result;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Statement\StatementListProvider;
use Wikibase\Lib\Summary;
/**
* Class for statement remove operation.
*
* @license GPL-2.0-or-later
* @author Addshore
* @author Thiemo Kreuz
*/
class ChangeOpRemoveStatement extends ChangeOpBase {
/**
* @var string
*/
private $guid;
/**
* @return string
*/
public function getGuid() {
return $this->guid;
}
/**
* Constructs a new mainsnak change operation
*
* @param string $guid
*
* @throws InvalidArgumentException
*/
public function __construct( $guid ) {
if ( !is_string( $guid ) || $guid === '' ) {
throw new InvalidArgumentException( '$guid must be a non-empty string' );
}
$this->guid = $guid;
}
/** @inheritDoc */
public function apply( EntityDocument $entity, Summary $summary = null ) {
if ( !( $entity instanceof StatementListProvider ) ) {
throw new InvalidArgumentException( '$entity must be a StatementListProvider' );
}
$statements = $entity->getStatements();
$statement = $statements->getFirstStatementWithGuid( $this->guid );
if ( $statement === null ) {
throw new ChangeOpException( "Entity does not have statement with GUID $this->guid" );
}
$statements->removeStatementsWithGuid( $this->guid );
$removedSnak = $statement->getMainSnak();
$this->updateSummary( $summary, 'remove', '', $this->getSummaryArgs( $removedSnak ) );
return new GenericChangeOpResult( $entity->getId(), true );
}
/**
* @param Snak $mainSnak
*
* @return array
*/
private function getSummaryArgs( Snak $mainSnak ) {
$propertyId = $mainSnak->getPropertyId();
return [ [ $propertyId->getSerialization() => $mainSnak ] ];
}
/**
* @see ChangeOp::validate
*
* @param EntityDocument $entity
*
* @return Result Always successful.
*/
public function validate( EntityDocument $entity ) {
//TODO: move validation logic from apply() here.
return Result::newSuccess();
}
}