/
MapLocationDistance.php
91 lines (82 loc) · 2.76 KB
/
MapLocationDistance.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);
namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\CustomFieldInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\Target\MapLocationTarget;
/**
* Sets sort direction on the MapLocation distance for a content query.
*/
class MapLocationDistance extends SortClause implements CustomFieldInterface
{
/**
* Custom fields to sort by instead of the default field.
*
* @var array
*/
protected $customFields = [];
/**
* Constructs a new MapLocationDistance SortClause on Type $typeIdentifier and Field $fieldIdentifier.
*
* @param string $typeIdentifier ContentType identifier
* @param string $fieldIdentifier FieldDefinition identifier
* @param float $latitude Latitude of the location that distance is calculated from
* @param float $longitude Longitude of the location that distance is calculated from
* @param string $sortDirection
*/
public function __construct(
string $typeIdentifier,
string $fieldIdentifier,
float $latitude,
float $longitude,
string $sortDirection = Query::SORT_ASC
) {
parent::__construct(
'maplocation_distance',
$sortDirection,
new MapLocationTarget(
$latitude,
$longitude,
$typeIdentifier,
$fieldIdentifier
)
);
}
/**
* Set a custom field to sort by.
*
* Set a custom field to sort by for a defined field in a defined type.
*
* @param string $type
* @param string $field
* @param string $customField
*/
public function setCustomField(string $type, string $field, string $customField): void
{
$this->customFields[$type][$field] = $customField;
}
/**
* Return custom field.
*
* If no custom field is set, return null
*
* @param string $type
* @param string $field
*
* @return mixed
*/
public function getCustomField(string $type, string $field): ?string
{
if (!isset($this->customFields[$type]) ||
!isset($this->customFields[$type][$field])) {
return null;
}
return $this->customFields[$type][$field];
}
}
class_alias(MapLocationDistance::class, 'eZ\Publish\API\Repository\Values\Content\Query\SortClause\MapLocationDistance');