Skip to content

Commit 710c306

Browse files
jakobwWMDE bot
authored andcommitted
Add NewStatement::withSubject()
This method is meant to be used in combination with `withSomeGuid()` and sets the prefix of the Statement ID. `withSomeGuid()` otherwise uses the Statement Property as the GUID prefix, which does not really make sense given that the Statement GUID is supposed to contain the *subject* ID. Change-Id: I579b7960d675871ea4d182c5df60d5e180e545c2
1 parent 5e745ab commit 710c306

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

tests/unit/NewStatement.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class NewStatement {
4949
/** @var string|bool|null */
5050
private $guid;
5151

52+
private ?string $subjectId = null;
53+
5254
/**
5355
* @var Snak[]
5456
*/
@@ -152,10 +154,7 @@ public function withGuid( $guid ) {
152154
return $result;
153155
}
154156

155-
/**
156-
* @return static
157-
*/
158-
public function withSomeGuid() {
157+
public function withSomeGuid(): static {
159158
$result = clone $this;
160159
if ( $result->guid !== null ) {
161160
throw new LogicException( 'Cannot redefine GUID' );
@@ -166,6 +165,17 @@ public function withSomeGuid() {
166165
return $result;
167166
}
168167

168+
public function withSubject( EntityId|string $subject ): static {
169+
$result = clone $this;
170+
if ( $result->subjectId !== null ) {
171+
throw new LogicException( 'Cannot redefine subject' );
172+
}
173+
174+
$result->subjectId = (string)$subject;
175+
176+
return $result;
177+
}
178+
169179
/**
170180
* @param string|NumericPropertyId $propertyId
171181
* @param DataValue|EntityId|string $value If not a DataValue object, the builder tries to
@@ -230,8 +240,10 @@ public function build() {
230240
$result->setRank( $this->rank );
231241

232242
if ( $this->guid === self::GENERATE_GUID ) {
243+
// defaulting to the Property ID here doesn't really make sense. withSomeGuid() should be used in combination with withSubject()
244+
$subject = $this->subjectId ?? $this->propertyId;
233245
$result->setGuid(
234-
$this->propertyId->getSerialization() . '$' . $this->generateUuidV4()
246+
"$subject\$" . $this->generateUuidV4()
235247
);
236248
} elseif ( $this->guid ) {
237249
$result->setGuid( $this->guid );

0 commit comments

Comments
 (0)