Completed
Push — master ( 8d42b2...8f0e5e )
by adam
02:14
created

src/Api/Service/StatementCreator.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Wikibase\Api\Service;
4
5
use Deserializers\Deserializer;
6
use Mediawiki\DataModel\EditInfo;
7
use Serializers\Serializer;
8
use UnexpectedValueException;
9
use Wikibase\Api\WikibaseApi;
10
use Wikibase\DataModel\Entity\EntityId;
11
use Wikibase\DataModel\Entity\Item;
12
use Wikibase\DataModel\Entity\Property;
13
use Wikibase\DataModel\Snak\PropertyValueSnak;
14
use Wikibase\DataModel\Snak\Snak;
15
16
/**
17
 * @access private
18
 *
19
 * @author Addshore
20
 */
21
class StatementCreator {
22
23
	/**
24
	 * @var WikibaseApi
25
	 */
26
	private $api;
27
28
	/**
29
	 * @var Deserializer
30
	 */
31
	private $dataValueSerializer;
32
33
	/**
34
	 * @param WikibaseApi $api
35
	 * @param Serializer $dataValueSerializer
36
	 */
37
	public function __construct( WikibaseApi $api, Serializer $dataValueSerializer ) {
38
		$this->api = $api;
39
		$this->dataValueSerializer = $dataValueSerializer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dataValueSerializer of type object<Serializers\Serializer> is incompatible with the declared type object<Deserializers\Deserializer> of property $dataValueSerializer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40
	}
41
42
	/**
43
	 * @since 0.2
44
	 *
45
	 * @param Snak $mainSnak
46
	 * @param EntityId|Item|Property|string $target
47
	 * @param EditInfo|null $editInfo
48
	 *
49
	 * @return string the GUID of the claim
50
	 * @throws UnexpectedValueException
51
	 */
52
	public function create( Snak $mainSnak, $target, EditInfo $editInfo = null ) {
53
		if( is_string( $target ) ) {
54
			$entityId = $target;
55
		} elseif ( $target instanceof EntityId ) {
56
			$entityId = $target->getSerialization();
57
		} elseif ( $target instanceof Item || $target instanceof Property ) {
58
			$entityId = $target->getId()->getSerialization();
59
		} else {
60
			throw new UnexpectedValueException( '$target needs to be an EntityId, Entity or string' );
61
		}
62
63
		$params = array(
64
			'entity' => $entityId,
65
			'snaktype' => $mainSnak->getType(),
66
			'property' => $mainSnak->getPropertyId()->getSerialization(),
67
		);
68
		if( $mainSnak instanceof PropertyValueSnak ) {
69
			$serializedDataValue = $this->dataValueSerializer->serialize( $mainSnak->getDataValue() );
0 ignored issues
show
The method serialize() does not exist on Deserializers\Deserializer. Did you maybe mean deserialize()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
70
			$params['value'] = json_encode( $serializedDataValue['value'] );
71
		}
72
73
		$result = $this->api->postRequest( 'wbcreateclaim', $params, $editInfo );
74
		return $result['claims']['id'];
75
	}
76
77
}