Add filters.

This commit is contained in:
David Molineus
2015-01-24 21:36:33 +01:00
parent 4c6f731d99
commit 05d3acf21d
3 changed files with 219 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Filter;
use Netzmacht\LeafletPHP\Definition\Type\LatLngBounds;
/**
* The Bounds box filter.
*
* @package Netzmacht\Contao\Leaflet\Filter
*/
class BboxFilter implements Filter
{
/**
* The bounds.
*
* @var LatLngBounds
*/
private $bounds;
/**
* Construct.
*
* @param LatLngBounds $bounds The bounds.
*/
public function __construct(LatLngBounds $bounds)
{
$this->bounds = $bounds;
}
/**
* {@inheritdoc}
*/
public static function getName()
{
return 'bbox';
}
/**
* {@inheritdoc}
*/
public static function fromRequest($request)
{
return new static(LatLngBounds::fromString($request));
}
/**
* {@inheritdoc}
*/
public function toRequest()
{
return $this->bounds->toString(true);
}
/**
* {@inheritdoc}
*/
public function getValues()
{
return array('bounds' => $this->bounds);
}
}

View File

@@ -0,0 +1,95 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Filter;
use Netzmacht\LeafletPHP\Definition\Type\LatLng;
/**
* Class DistanceFilter filters by a coordinate the the distance from it.
*
* @package Netzmacht\Contao\Leaflet\Filter
*/
class DistanceFilter implements Filter
{
/**
* The center coordinates.
*
* @var LatLng
*/
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
);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/**
* @package dev
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2015 netzmacht creative David Molineus
* @license LGPL 3.0
* @filesource
*
*/
namespace Netzmacht\Contao\Leaflet\Filter;
/**
* Interface Filter is designed for filter a data request. It just contains the filter information.
*
* Each layer has it own responsibility to apply the filter to their provided data.
*
* @package Netzmacht\Contao\Leaflet\Filter
*/
interface Filter
{
/**
* Get the name of the filter.
*
* @return string
*/
public static function getName();
/**
* Create the filter from a request string.
*
* @param string $request The request.
*
* @return Filter
*/
public static function fromRequest($request);
/**
* Create request string representation.
*
* @return string
*/
public function toRequest();
/**
* Get the param values.
*
* @return array
*/
public function getValues();
}