Skip to content

Commit b48bda0

Browse files
author
Daniel Kinzler
committed
Merge pull request #508 from wmde/epvs
Implement ExtendedPropertyValueSnak
2 parents 5d00935 + 0f8eaad commit b48bda0

3 files changed

Lines changed: 165 additions & 1 deletion

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Snak;
4+
5+
use DataValues\DataValue;
6+
use InvalidArgumentException;
7+
use Wikibase\DataModel\Entity\PropertyId;
8+
9+
/**
10+
* PropertyValueSnak with with derived values attached.
11+
* Derived DataValues can be used to provide infomation, for example, normalized values.
12+
*
13+
* Calls to the getType method will indicate that this is a PropertyValueSnak
14+
*
15+
* Direct serialization of this object will not include the extra derived values
16+
*
17+
* The hash of this object is not changed by the extra Derived values
18+
* thus this object will ->equal a PropertyValueSnak of the same value.
19+
*
20+
* @since 3.1
21+
*
22+
* @licence GNU GPL v2+
23+
* @author Adam Shorland
24+
*/
25+
class DerivedPropertyValueSnak extends PropertyValueSnak {
26+
27+
/**
28+
* @var DataValue[]
29+
*/
30+
private $derivedDataValues = array();
31+
32+
/**
33+
* @param PropertyId $propertyId
34+
* @param DataValue $dataValue
35+
* @param DataValue[] $derivedDataValues
36+
*/
37+
public function __construct(
38+
PropertyId $propertyId,
39+
DataValue $dataValue,
40+
array $derivedDataValues
41+
) {
42+
parent::__construct( $propertyId, $dataValue );
43+
44+
foreach ( $derivedDataValues as $key => $extensionDataValue ) {
45+
if ( !( $extensionDataValue instanceof DataValue ) || !is_string( $key ) ) {
46+
throw new InvalidArgumentException(
47+
'$derivedDataValues must be an array of DataValue objects with string keys'
48+
);
49+
}
50+
}
51+
52+
$this->derivedDataValues = $derivedDataValues;
53+
}
54+
55+
/**
56+
* @return DataValue[] with string keys
57+
*/
58+
public function getDerivedDataValues() {
59+
return $this->derivedDataValues;
60+
}
61+
62+
/**
63+
* @param string $key
64+
*
65+
* @return DataValue|null
66+
*/
67+
public function getDerivedDataValue( $key ) {
68+
if ( isset( $this->derivedDataValues[$key] ) ) {
69+
return $this->derivedDataValues[$key];
70+
}
71+
return null;
72+
}
73+
74+
}

src/Snak/PropertyValueSnak.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class PropertyValueSnak extends SnakObject {
2020

21-
private $dataValue;
21+
protected $dataValue;
2222

2323
/**
2424
* Support for passing in an EntityId instance that is not a PropertyId instance has
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Wikibase\DataModel\Tests\Snak;
4+
5+
use DataValues\StringValue;
6+
use Wikibase\DataModel\Entity\PropertyId;
7+
use Wikibase\DataModel\Snak\DerivedPropertyValueSnak;
8+
9+
/**
10+
* @covers Wikibase\DataModel\Snak\DerivedPropertyValueSnak
11+
*
12+
* @group Wikibase
13+
* @group WikibaseDataModel
14+
* @group WikibaseSnak
15+
*
16+
* @licence GNU GPL v2+
17+
* @author Adam Shorland
18+
*/
19+
class DerivedPropertyValueSnakTest extends SnakObjectTest {
20+
21+
public function constructorProvider() {
22+
return array(
23+
'No extras' => array(
24+
true,
25+
new PropertyId( 'P1' ),
26+
new StringValue( 'a' ),
27+
array(),
28+
),
29+
'2 extras' => array(
30+
true,
31+
new PropertyId( 'P9001' ),
32+
new StringValue( 'bc' ),
33+
array( 'foo' => new StringValue( 'foo' ), 'bar' => new StringValue( 'bar' ) ),
34+
),
35+
'fail - Integer key' => array(
36+
'InvalidArgumentException',
37+
new PropertyId( 'P9001' ),
38+
new StringValue( 'bc' ),
39+
array( new StringValue( 'foo' ) ),
40+
),
41+
'fail - not a value' => array(
42+
'InvalidArgumentException',
43+
new PropertyId( 'P9001' ),
44+
new StringValue( 'bc' ),
45+
array( 'foo' => 'bar' ),
46+
),
47+
);
48+
}
49+
50+
public function getClass() {
51+
return 'Wikibase\DataModel\Snak\DerivedPropertyValueSnak';
52+
}
53+
54+
/**
55+
* This test is a safeguard to make sure hashes are not changed unintentionally.
56+
*/
57+
public function testHashStability() {
58+
$snak = new DerivedPropertyValueSnak(
59+
new PropertyId( 'P1' ),
60+
new StringValue( 'a' ),
61+
array( 'foo' => new StringValue( 'foo' ) )
62+
);
63+
$hash = $snak->getHash();
64+
65+
// @codingStandardsIgnoreStart
66+
$expected = sha1( 'C:48:"Wikibase\DataModel\Snak\DerivedPropertyValueSnak":53:{a:2:{i:0;i:1;i:1;C:22:"DataValues\StringValue":1:{a}}}' );
67+
// @codingStandardsIgnoreEnd
68+
$this->assertSame( $expected, $hash );
69+
}
70+
71+
/**
72+
* @dataProvider instanceProvider
73+
*/
74+
public function testGetDerivedDataValues( DerivedPropertyValueSnak $snak, $args ) {
75+
$dataValues = $snak->getDerivedDataValues();
76+
$this->assertEquals( $args[2], $dataValues );
77+
}
78+
79+
/**
80+
* @dataProvider instanceProvider
81+
*/
82+
public function testGetDerivedDataValue( DerivedPropertyValueSnak $snak, $args ) {
83+
$this->assertEquals( null, $snak->getDerivedDataValue( 'ponies and unicorns' ) );
84+
foreach ( $args[2] as $expectedKey => $expectedDataValue ) {
85+
$dataValue = $snak->getDerivedDataValue( $expectedKey );
86+
$this->assertEquals( $expectedDataValue, $dataValue );
87+
}
88+
}
89+
90+
}

0 commit comments

Comments
 (0)