Skip to content

Commit fe27820

Browse files
author
Adam Hutchison
committed
Adds method to check order by direction
1 parent db6151b commit fe27820

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Netsells\GeoScope\Exceptions;
4+
5+
class InvalidOrderDirectionParameter extends \Exception
6+
{
7+
8+
}

src/Interfaces/ScopeDriverInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ public function withinDistanceOf(float $lat, float $long, float $distance);
2121
* Should return query instance
2222
*/
2323
public function orWithinDistanceOf(float $lat, float $long, float $distance);
24+
25+
/**
26+
* @param float $lat
27+
* @param float $long
28+
* @param string $orderDirection - asc or desc
29+
* @return mixed
30+
* Should return query instance
31+
*/
32+
public function orderByDistanceFrom(float $lat, float $long, string $orderDirection = 'asc');
2433
}

src/ScopeDrivers/AbstractScopeDriver.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Netsells\GeoScope\ScopeDrivers;
44

5+
use Netsells\GeoScope\Exceptions\InvalidOrderDirectionParameter;
56
use Netsells\GeoScope\Interfaces\ScopeDriverInterface;
67

78
abstract class AbstractScopeDriver implements ScopeDriverInterface
89
{
10+
protected const ALLOWED_ORDER_DIRECTION_IDENTIFIERS = ['asc', 'desc'];
11+
912
protected $config;
1013
protected $query;
1114
protected $conversion;
@@ -38,4 +41,15 @@ public function setQuery($query): AbstractScopeDriver
3841
$this->query = $query;
3942
return $this;
4043
}
44+
45+
/**
46+
* @throws InvalidOrderDirectionParameter
47+
* @return void
48+
*/
49+
protected function checkOrderDirectionIdentifier(string $orderDirection): void
50+
{
51+
if (!in_array($orderDirection, $this->ALLOWED_ORDER_DIRECTION_IDENTIFIERS)) {
52+
throw new InvalidOrderDirectionParameter("{$orderDirection} is not a valid order direction");
53+
}
54+
}
4155
}

src/ScopeDrivers/MySQLScopeDriver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public function orWithinDistanceOf(float $lat, float $long, float $distance)
3232
]);
3333
}
3434

35+
/**
36+
* @param float $lat
37+
* @param float $long
38+
* @param float $orderDirection
39+
*/
40+
public function orderByDistanceFrom(float $lat, float $long, string $orderDirection = 'asc')
41+
{
42+
return $this->query;
43+
}
44+
3545
/**
3646
* @return string
3747
*/

0 commit comments

Comments
 (0)