1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wolnosciowiec\CoordinatesBundle\Model\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Wolnosciowiec\CoordinatesBundle\Model\Entity\Coordinates\Coordinates; |
7
|
|
|
use Wolnosciowiec\CoordinatesBundle\Model\Entity\Coordinates\FieldsMapping; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Adds a possibility to filter objects |
11
|
|
|
* by distance from a point on the map |
12
|
|
|
* |
13
|
|
|
* @package Wolnosciowiec\CoordinatesBundle\Model\Repository |
14
|
|
|
*/ |
15
|
|
|
trait CoordinatesFeaturedRepositoryTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return FieldsMapping |
19
|
|
|
*/ |
20
|
|
|
abstract protected function getCoordinatesFieldsMapping(): FieldsMapping; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Appends a coordinates radius condition to the QueryBuilder |
25
|
|
|
* |
26
|
|
|
* @param QueryBuilder $query |
27
|
|
|
* @param Coordinates $coordinates |
28
|
|
|
*/ |
29
|
|
|
protected function appendQueryBuilderCoordinatesRadiusSearch( |
30
|
|
|
QueryBuilder $query, |
31
|
|
|
Coordinates $coordinates |
32
|
|
|
) |
33
|
|
|
{ |
34
|
|
|
// bounding box |
35
|
|
|
$boundingBox = $coordinates->getBoundingSquare(); |
36
|
|
|
$query->setParameter('c_dMin', $boundingBox->getLongitude()[0]); |
37
|
|
|
$query->setParameter('c_dMax', $boundingBox->getLongitude()[1]); |
38
|
|
|
$query->setParameter('c_sMin', $boundingBox->getLatitude()[0]); |
39
|
|
|
$query->setParameter('c_sMax', $boundingBox->getLatitude()[1]); |
40
|
|
|
|
41
|
|
|
// point |
42
|
|
|
$query->setParameter('c_lat', $coordinates->getLatitude()); |
43
|
|
|
$query->setParameter('c_lon', $coordinates->getLongitude()); |
44
|
|
|
|
45
|
|
|
// distance, radius |
46
|
|
|
$query->setParameter('c_od', pow($coordinates->getLatitudeInMeters(), 2)); |
47
|
|
|
$query->setParameter('c_os', pow($coordinates->getLongtitudeUnit(), 2)); |
48
|
|
|
$query->setParameter('c_radiusPOW', pow($coordinates->getDistanceInMeters(), 2)); |
49
|
|
|
|
50
|
|
|
$query->andWhere('(' . $this->getCoordinatesFieldsMapping()->getLatitudeFieldName() . ' > :c_sMin AND ' . $this->getCoordinatesFieldsMapping()->getLatitudeFieldName() . ' < :c_sMax AND ' . $this->getCoordinatesFieldsMapping()->getLongitudeFieldName() . ' < :c_dMax AND ' . $this->getCoordinatesFieldsMapping()->getLongitudeFieldName() . ' > :c_dMin)'); |
51
|
|
|
$query->andWhere('(abs(' . $this->getCoordinatesFieldsMapping()->getLongitudeFieldName() . ' - :c_lon) * abs(' . $this->getCoordinatesFieldsMapping()->getLongitudeFieldName() . ' - :c_lon) * :c_od + abs(' . $this->getCoordinatesFieldsMapping()->getLatitudeFieldName() . ' - :c_lat) * abs(' . $this->getCoordinatesFieldsMapping()->getLatitudeFieldName() . ' - :c_lat) * :c_os) < :c_radiusPOW'); |
52
|
|
|
} |
53
|
|
|
} |