|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Domain\Geolocation\Specification\Constraint; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Selector\SelectorInterface; |
|
14
|
|
|
use Cubiche\Core\Specification\Specification; |
|
15
|
|
|
use Cubiche\Domain\Geolocation\Coordinate; |
|
16
|
|
|
use Cubiche\Domain\Geolocation\Distance; |
|
17
|
|
|
use Cubiche\Domain\Geolocation\DistanceUnit; |
|
18
|
|
|
use Cubiche\Domain\Geolocation\GeolocalizableInterface; |
|
19
|
|
|
use Cubiche\Domain\System\Real; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Near Specification Class. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class Near extends Specification |
|
27
|
|
|
{ |
|
28
|
|
|
const DEFAULT_RADIUS = 50000; //m |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var SelectorInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $selector; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Coordinate |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $coordinate; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Distance |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $radius; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param SelectorInterface $selector |
|
47
|
|
|
* @param Coordinate $coordinate |
|
48
|
|
|
* @param Distance $radius |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(SelectorInterface $selector, Coordinate $coordinate, Distance $radius = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->selector = $selector; |
|
53
|
|
|
$this->coordinate = $coordinate; |
|
54
|
|
|
$this->radius = $radius === null ? |
|
55
|
|
|
new Distance(Real::fromNative(self::DEFAULT_RADIUS), DistanceUnit::METER()) : |
|
56
|
|
|
$radius; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return \Cubiche\Domain\Specification\SelectorInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function selector() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->selector; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return \Cubiche\Domain\Geolocation\Coordinate |
|
69
|
|
|
*/ |
|
70
|
|
|
public function coordinate() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->coordinate; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return \Cubiche\Domain\Geolocation\Distance |
|
77
|
|
|
*/ |
|
78
|
|
|
public function radius() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->radius; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function evaluate($value) |
|
87
|
|
|
{ |
|
88
|
|
|
$geolocalizable = $this->selector->apply($value); |
|
89
|
|
|
if (!$geolocalizable instanceof GeolocalizableInterface) { |
|
90
|
|
|
throw new \LogicException(\sprintf( |
|
91
|
|
|
'%s not implement %s, the near operator is defined only to %s instances', |
|
92
|
|
|
\is_object($geolocalizable) ? \gettype($geolocalizable) : \get_class($geolocalizable), |
|
93
|
|
|
GeolocalizableInterface::class, |
|
94
|
|
|
GeolocalizableInterface::class |
|
95
|
|
|
)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $geolocalizable->coordinate() !== null && |
|
99
|
|
|
$geolocalizable->coordinate()->distance($this->coordinate())->compareTo($this->radius()) <= 0; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|