Skip to content

Commit 1b5e15a

Browse files
committed
Adds support for AND and OR
1 parent 8e2524e commit 1b5e15a

File tree

8 files changed

+236
-2
lines changed

8 files changed

+236
-2
lines changed

src/Query/AndQuery.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace WikidataQueryApi\Query;
4+
5+
/**
6+
* @licence GPLv2+
7+
* @author Thomas Pellissier Tanon
8+
*/
9+
class AndQuery extends CollectionQuery {
10+
11+
/**
12+
* @see AbstractQuery::getType
13+
*/
14+
public function getType() {
15+
return 'AND';
16+
}
17+
}

src/Query/CollectionQuery.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace WikidataQueryApi\Query;
4+
5+
/**
6+
* @licence GPLv2+
7+
* @author Thomas Pellissier Tanon
8+
*/
9+
abstract class CollectionQuery extends AbstractQuery {
10+
11+
/**
12+
* @var AbstractQuery[]
13+
*/
14+
private $subQueries;
15+
16+
/**
17+
* @param AbstractQuery[] $subQueries
18+
*/
19+
public function __construct( array $subQueries ) {
20+
$this->subQueries = $subQueries;
21+
}
22+
23+
/**
24+
* @return AbstractQuery[]
25+
*/
26+
public function getSubQueries() {
27+
return $this->subQueries;
28+
}
29+
}

src/Query/OrQuery.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace WikidataQueryApi\Query;
4+
5+
/**
6+
* @licence GPLv2+
7+
* @author Thomas Pellissier Tanon
8+
*/
9+
class OrQuery extends CollectionQuery {
10+
11+
/**
12+
* @see AbstractQuery::getType
13+
*/
14+
public function getType() {
15+
return 'OR';
16+
}
17+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace WikidataQueryApi\Query\Serializers;
4+
5+
use Serializers\DispatchableSerializer;
6+
use Serializers\Exceptions\UnsupportedObjectException;
7+
use Serializers\Serializer;
8+
use WikidataQueryApi\Query\AbstractQuery;
9+
use WikidataQueryApi\Query\CollectionQuery;
10+
11+
/**
12+
* @licence GPLv2+
13+
* @author Thomas Pellissier Tanon
14+
*/
15+
class QuerySerializer implements DispatchableSerializer {
16+
17+
/**
18+
* @var Serializer
19+
*/
20+
private $commandsSerializer;
21+
22+
/**
23+
* @param Serializer $commandsSerializer
24+
*/
25+
public function __construct( Serializer $commandsSerializer ) {
26+
$this->commandsSerializer = $commandsSerializer;
27+
}
28+
29+
/**
30+
* @see DispatchableSerializer::isSerializerFor
31+
*/
32+
public function isSerializerFor( $object ) {
33+
return is_object( $object ) && $object instanceof AbstractQuery;
34+
}
35+
36+
/**
37+
* @see Serializer::serialize
38+
*/
39+
public function serialize( $object ) {
40+
if ( !$this->isSerializerFor( $object ) ) {
41+
throw new UnsupportedObjectException(
42+
$object,
43+
'QuerySerializer can only serialize AbstractQuery objects'
44+
);
45+
}
46+
47+
return $this->getSerialized( $object );
48+
}
49+
50+
private function getSerialized( AbstractQuery $query ) {
51+
if ( $query instanceof CollectionQuery ) {
52+
return '(' . implode( ' ' . $query->getType() . ' ', $this->serializeQueries( $query->getSubQueries() ) ) . ')';
53+
}
54+
55+
return $this->commandsSerializer->serialize( $query );
56+
}
57+
58+
private function serializeQueries( array $queries ) {
59+
$serialized = array();
60+
61+
foreach( $queries as $query ) {
62+
$serialized[] = $this->getSerialized( $query );
63+
}
64+
65+
return $serialized;
66+
}
67+
}

src/WikidataQueryFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use WikidataQueryApi\Query\Serializers\AroundQuerySerializer;
77
use WikidataQueryApi\Query\Serializers\BetweenQuerySerializer;
88
use WikidataQueryApi\Query\Serializers\ClaimQuerySerializer;
9+
use WikidataQueryApi\Query\Serializers\QuerySerializer;
910
use WikidataQueryApi\Query\Serializers\StringQuerySerializer;
1011
use WikidataQueryApi\Services\SimpleQueryService;
1112

@@ -35,11 +36,11 @@ public function newSimpleQueryService() {
3536
}
3637

3738
private function newQuerySerializer() {
38-
return new DispatchingSerializer( array(
39+
return new QuerySerializer( new DispatchingSerializer( array(
3940
new ClaimQuerySerializer(),
4041
new StringQuerySerializer(),
4142
new AroundQuerySerializer(),
4243
new BetweenQuerySerializer()
43-
) );
44+
) ) );
4445
}
4546
}

tests/unit/Query/AndQueryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace WikidataQueryApi\Query;
4+
5+
/**
6+
* @covers WikidataQueryApi\Query\AndQuery
7+
*
8+
* @licence GPLv2+
9+
* @author Thomas Pellissier Tanon
10+
*/
11+
class AndQueryTest extends \PHPUnit_Framework_TestCase {
12+
13+
public function testGetSubQueries() {
14+
$query = new AndQuery( array() );
15+
$this->assertEquals( array(), $query->getSubQueries() );
16+
}
17+
18+
public function testGetType() {
19+
$query = new AndQuery( array() );
20+
$this->assertEquals( 'AND', $query->getType() );
21+
}
22+
}

tests/unit/Query/OrQueryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace WikidataQueryApi\Query;
4+
5+
/**
6+
* @covers WikidataQueryApi\Query\OrQuery
7+
*
8+
* @licence GPLv2+
9+
* @author Thomas Pellissier Tanon
10+
*/
11+
class OrQueryTest extends \PHPUnit_Framework_TestCase {
12+
13+
public function testGetSubQueries() {
14+
$query = new OrQuery( array() );
15+
$this->assertEquals( array(), $query->getSubQueries() );
16+
}
17+
18+
public function testGetType() {
19+
$query = new OrQuery( array() );
20+
$this->assertEquals( 'OR', $query->getType() );
21+
}
22+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace WikidataQueryApi\Query\Serializers;
4+
5+
use DataValues\StringValue;
6+
use Wikibase\DataModel\Entity\PropertyId;
7+
use WikidataQueryApi\Query\AndQuery;
8+
use WikidataQueryApi\Query\StringQuery;
9+
10+
/**
11+
* @covers WikidataQueryApi\Query\Serializers\QuerySerializer
12+
*
13+
* @licence GPLv2+
14+
* @author Thomas Pellissier Tanon
15+
*/
16+
class QuerySerializerTest extends SerializerBaseTest {
17+
18+
public function buildSerializer() {
19+
return new QuerySerializer( new StringQuerySerializer() );
20+
}
21+
22+
public function serializableProvider() {
23+
return array(
24+
array(
25+
new StringQuery( new PropertyId( 'P42' ), new StringValue( 'foo' ) )
26+
),
27+
array(
28+
new AndQuery( array( new StringQuery( new PropertyId( 'P42' ), new StringValue( 'foo' ) ) ) )
29+
),
30+
);
31+
}
32+
33+
public function nonSerializableProvider() {
34+
return array(
35+
array(
36+
5
37+
),
38+
array(
39+
array()
40+
),
41+
);
42+
}
43+
44+
public function serializationProvider() {
45+
return array(
46+
array(
47+
'string[42:"foo"]',
48+
new StringQuery( new PropertyId( 'P42' ), new StringValue( 'foo' ) )
49+
),
50+
array(
51+
'(string[42:"foo"] AND string[43:"bar"])',
52+
new AndQuery(array(
53+
new StringQuery( new PropertyId( 'P42' ), new StringValue( 'foo' ) ),
54+
new StringQuery( new PropertyId( 'P43' ), new StringValue( 'bar' ) )
55+
) )
56+
),
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)