1 | <?php |
||
32 | class MongoDBEntityIdForQueryLookup implements EntityIdForQueryLookup { |
||
33 | |||
34 | /** |
||
35 | * @var Database |
||
36 | */ |
||
37 | private $database; |
||
38 | |||
39 | /** |
||
40 | * @var MongoDBDocumentBuilder |
||
41 | */ |
||
42 | private $documentBuilder; |
||
43 | |||
44 | /** |
||
45 | * @var int|null |
||
46 | */ |
||
47 | private $timeLimit; |
||
48 | |||
49 | /** |
||
50 | * @param Database $database |
||
51 | * @param MongoDBDocumentBuilder $documentBuilder |
||
52 | * @param int|null $timeLimit |
||
53 | */ |
||
54 | 16 | public function __construct( Database $database, MongoDBDocumentBuilder $documentBuilder, $timeLimit = null ) { |
|
55 | 16 | $this->database = $database; |
|
56 | 16 | $this->documentBuilder = $documentBuilder; |
|
57 | 16 | $this->timeLimit = $timeLimit; |
|
58 | 16 | } |
|
59 | |||
60 | /** |
||
61 | * @see EntityForQueryLookup::getEntityIdsForQuery |
||
62 | */ |
||
63 | 16 | public function getEntityIdsForQuery( Description $queryDescription, QueryOptions $queryOptions = null, $entityType ) { |
|
66 | |||
67 | 16 | private function doQuery( Description $queryDescription, QueryOptions $queryOptions = null, $entityType ) { |
|
68 | 16 | $cursor = $this->database |
|
69 | 16 | ->selectCollection( $entityType ) |
|
70 | 16 | ->find( |
|
71 | 16 | $this->buildQueryForDescription( $queryDescription, new Expr() )->getQuery(), |
|
72 | 9 | $this->buildQueryModifiers() |
|
73 | 9 | ); |
|
74 | |||
75 | 9 | if( $queryOptions === null ) { |
|
76 | 1 | return $cursor; |
|
77 | } |
||
78 | |||
79 | 8 | return $this->applyOptionsToCursor( $cursor, $queryOptions ); |
|
80 | } |
||
81 | |||
82 | 16 | private function buildQueryForDescription( Description $description, Expr $expr, PropertyId $currentProperty = null ) { |
|
83 | 16 | if( $description instanceof AnyValue ) { |
|
84 | 4 | return $expr; |
|
85 | 14 | } elseif( $description instanceof Conjunction ) { |
|
86 | 2 | return $this->buildQueryForConjunction( $description, $expr, $currentProperty ); |
|
87 | 14 | } elseif( $description instanceof Disjunction ) { |
|
88 | 2 | return $this->buildQueryForDisjunction( $description, $expr, $currentProperty ); |
|
89 | 14 | } elseif( $description instanceof SomeProperty ) { |
|
90 | 13 | return $this->buildQueryForSomeProperty( $description, $expr ); |
|
91 | 11 | } elseif( $description instanceof ValueDescription ) { |
|
92 | 10 | return $this->buildQueryForValueDescription( $description, $expr, $currentProperty ); |
|
93 | } else { |
||
94 | 1 | throw new FeatureNotSupportedException( 'Unknown description type: ' . $description->getType() ); |
|
95 | } |
||
96 | } |
||
97 | |||
98 | 2 | private function buildQueryForConjunction( Conjunction $conjunction, Expr $expr, PropertyId $currentProperty = null ) { |
|
99 | 2 | foreach( $conjunction->getDescriptions() as $description ) { |
|
100 | 2 | $expr->addAnd( $this->buildQueryForDescription( $description, new Expr(), $currentProperty ) ); |
|
101 | 2 | } |
|
102 | 2 | return $expr; |
|
103 | } |
||
104 | |||
105 | 2 | private function buildQueryForDisjunction( Disjunction $disjunction, Expr $expr, PropertyId $currentProperty = null ) { |
|
106 | 2 | foreach( $disjunction->getDescriptions() as $description ) { |
|
107 | 2 | $expr->addOr( $this->buildQueryForDescription( $description, new Expr(), $currentProperty ) ); |
|
108 | 2 | } |
|
109 | 2 | return $expr; |
|
110 | } |
||
111 | |||
112 | 13 | private function buildQueryForSomeProperty( SomeProperty $someProperty, Expr $expr ) { |
|
129 | |||
130 | 10 | private function buildQueryForValueDescription( |
|
131 | ValueDescription $valueDescription, |
||
132 | Expr $expr, |
||
133 | PropertyId $currentProperty = null |
||
134 | ) { |
||
135 | 10 | $value = $valueDescription->getValue(); |
|
136 | |||
137 | 10 | switch( $valueDescription->getComparator() ) { |
|
138 | 10 | case ValueDescription::COMP_EQUAL: |
|
139 | 10 | case ValueDescription::COMP_LIKE: |
|
140 | 9 | $expr->field( 'sclaims.' . $value->getType() )->equals( $this->buildPropertyValueForSearch( $currentProperty, $value ) ); |
|
|
|||
141 | 7 | return $expr; |
|
142 | |||
143 | 1 | default: |
|
144 | 1 | throw new FeatureNotSupportedException( 'Unsupported ValueDescription comparator' ); |
|
145 | 1 | } |
|
146 | } |
||
147 | |||
148 | 9 | private function buildPropertyValueForSearch( PropertyId $propertyId, DataValue $dataValue ) { |
|
159 | |||
160 | 5 | private function buildEntityIdValueForSearch( PropertyId $propertyId, EntityIdValue $entityIdValue ) { |
|
161 | 5 | $entityId = $entityIdValue->getEntityId(); |
|
162 | |||
163 | 5 | if( !( $entityId instanceof ItemId || $entityId instanceof PropertyId ) ) { |
|
164 | 1 | throw new FeatureNotSupportedException( 'Not supported entity type: ' . $entityId->getEntityType() ); |
|
165 | } |
||
166 | |||
167 | 4 | return $propertyId->getSerialization() . '-' . $entityIdValue->getEntityId()->getSerialization(); |
|
168 | } |
||
169 | |||
170 | 4 | private function buildStringValueForSearch( PropertyId $propertyId, StringValue $stringValue ) { |
|
174 | |||
175 | 2 | private function buildTimeValueForSearch( PropertyId $propertyId, TimeValue $timeValue ) { |
|
176 | 2 | $significantTimePart = preg_replace( '/(-00)*T00:00:00Z$/', '', $timeValue->getTime() ); |
|
177 | |||
178 | 2 | return new MongoRegex( '/^' . preg_quote( $propertyId->getSerialization() . '-' . $significantTimePart, '/' ) . '/' ); |
|
179 | } |
||
180 | |||
181 | 9 | private function buildQueryModifiers() { |
|
182 | 9 | $modifiers = [ '_id' => 1 ]; |
|
190 | |||
191 | 8 | private function applyOptionsToCursor( Cursor $cursor, QueryOptions $options ) { |
|
200 | |||
201 | 9 | private function formatResults( Iterator $cursor ) { |
|
210 | } |
||
211 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):