Files
contao-leaflet-maps/src/Filter/DistanceFilter.php

97 lines
2.0 KiB
PHP
Raw Normal View History

2015-01-24 21:36:33 +01:00
<?php
/**
2017-10-05 15:45:43 +02:00
* Leaflet maps for Contao CMS.
*
2016-10-11 10:40:15 +02:00
* @package contao-leaflet-maps
2015-01-24 21:36:33 +01:00
* @author David Molineus <david.molineus@netzmacht.de>
2017-10-05 15:45:43 +02:00
* @copyright 2016-2017 netzmacht David Molineus. All rights reserved.
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
2015-01-24 21:36:33 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Filter;
use Netzmacht\LeafletPHP\Value\LatLng;
2015-01-24 21:36:33 +01:00
/**
* Class DistanceFilter filters by a coordinate the the distance from it.
*
* @package Netzmacht\Contao\Leaflet\Filter
*/
class DistanceFilter implements Filter
{
/**
* The center coordinates.
*
* @var \Netzmacht\LeafletPHP\Value\LatLng
2015-01-24 21:36:33 +01:00
*/
private $center;
/**
* The radius in meters.
*
* @var int
*/
private $radius;
/**
* Construct.
*
* @param LatLng $center The center coordinates.
* @param int $radius The radius in meters.
*/
public function __construct(LatLng $center, $radius)
{
$this->center = $center;
$this->radius = (int) $radius;
}
/**
* {@inheritdoc}
*/
public static function getName()
{
return 'distance';
}
/**
* {@inheritdoc}
*
* @throws \InvalidArgumentException If the value could not be parsed.
*/
public static function fromRequest($request)
{
$values = explode(',', $request, 3);
if (count($values) !== 3) {
throw new \InvalidArgumentException(sprintf('Invalid request value "%s"', $request));
}
return new static(
new LatLng($values[0], $values[1]),
$values[2]
);
}
/**
* {@inheritdoc}
*/
public function toRequest()
{
return $this->center->toString(true) . ',' . $this->radius;
}
/**
* {@inheritdoc}
*/
public function getValues()
{
return array(
'radius' => $this->radius,
'center' => $this->center
);
}
}