Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| CirrusNearCoordFilterFeature | |
100.00% |
12 / 12 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| getKeywords | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doApply | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| parseValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| doGetFilterquery | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getFilterQuery | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace GeoData\Search; |
| 4 | |
| 5 | use CirrusSearch\Parser\AST\KeywordFeatureNode; |
| 6 | use CirrusSearch\Query\Builder\QueryBuildingContext; |
| 7 | use CirrusSearch\Query\FilterQueryFeature; |
| 8 | use CirrusSearch\Query\SimpleKeywordFeature; |
| 9 | use CirrusSearch\Search\SearchContext; |
| 10 | use CirrusSearch\WarningCollector; |
| 11 | use Elastica\Query\AbstractQuery; |
| 12 | use GeoData\Coord; |
| 13 | |
| 14 | /** |
| 15 | * Applies geo filtering to the query by providing coordinates. |
| 16 | * |
| 17 | * Limits search results to a geographic area within the geographic area. All values |
| 18 | * can be prefixed with a radius in m or km to apply. If not specified this defaults |
| 19 | * to 5km. |
| 20 | * |
| 21 | * Examples: |
| 22 | * nearcoord:1.2345,-5.4321 |
| 23 | * nearcoord:17km,54.321,-12.345 |
| 24 | */ |
| 25 | class CirrusNearCoordFilterFeature extends SimpleKeywordFeature implements FilterQueryFeature { |
| 26 | use CirrusGeoFeature; |
| 27 | |
| 28 | /** |
| 29 | * @return string[] |
| 30 | */ |
| 31 | protected function getKeywords() { |
| 32 | return [ 'nearcoord' ]; |
| 33 | } |
| 34 | |
| 35 | /** @inheritDoc */ |
| 36 | protected function doApply( SearchContext $context, $key, $value, $quotedValue, $negated ) { |
| 37 | [ $coord, $radius ] = $this->parseValue( $key, $value, $quotedValue, |
| 38 | '', '', $context ); |
| 39 | return [ $this->doGetFilterquery( $coord, $radius ), false ]; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param string $key |
| 44 | * @param string $value |
| 45 | * @param string $quotedValue |
| 46 | * @param string $valueDelimiter |
| 47 | * @param string $suffix |
| 48 | * @param WarningCollector $warningCollector |
| 49 | * @return array{array{lat:float,lon:float,globe:string}|null,int} |
| 50 | */ |
| 51 | public function parseValue( |
| 52 | $key, |
| 53 | $value, |
| 54 | $quotedValue, |
| 55 | $valueDelimiter, |
| 56 | $suffix, |
| 57 | WarningCollector $warningCollector |
| 58 | ) { |
| 59 | return $this->parseGeoNearby( $warningCollector, $key, $value ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param array|null $coord |
| 64 | * @param int $radius |
| 65 | * @return AbstractQuery|null |
| 66 | */ |
| 67 | protected function doGetFilterquery( $coord, $radius ) { |
| 68 | $filter = null; |
| 69 | if ( $coord !== null ) { |
| 70 | $coordObject = new Coord( $coord['lat'], $coord['lon'], $coord['globe'] ); |
| 71 | $filter = CirrusNearTitleFilterFeature::createQuery( $coordObject, $radius ); |
| 72 | } |
| 73 | |
| 74 | return $filter; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @param KeywordFeatureNode $node |
| 79 | * @param QueryBuildingContext $context |
| 80 | * @return AbstractQuery|null |
| 81 | */ |
| 82 | public function getFilterQuery( KeywordFeatureNode $node, QueryBuildingContext $context ) { |
| 83 | [ $coord, $radius ] = $node->getParsedValue(); |
| 84 | return $this->doGetFilterquery( $coord, $radius ); |
| 85 | } |
| 86 | } |