FEATURE: Add GeoPoint Processor

Document data processors.
Add test for new data processor.
This commit is contained in:
Daniel Siepmann 2017-10-20 16:36:26 +02:00
parent 3f1c3db2a2
commit e1764dca13
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
6 changed files with 198 additions and 10 deletions

View file

@ -25,7 +25,7 @@ namespace Codappix\SearchCore\DataProcessing;
*/
class CopyToProcessor implements ProcessorInterface
{
public function processRecord(array $record, array $configuration)
public function processRecord(array $record, array $configuration) : array
{
$all = [];
@ -36,10 +36,6 @@ class CopyToProcessor implements ProcessorInterface
return $record;
}
/**
* @param array &$target
* @param array $from
*/
protected function addArray(array &$target, array $from)
{
foreach ($from as $value) {

View file

@ -20,15 +20,40 @@ namespace Codappix\SearchCore\DataProcessing;
* 02110-1301, USA.
*/
class GeoPointProcessing implements ProcessorInterface
/**
* Adds a new fields, ready to use as GeoPoint field for Elasticsearch.
*/
class GeoPointProcessor implements ProcessorInterface
{
public function processRecord(array $record, array $configuration) : array
{
if (! $this->canApply($record, $configuration)) {
return $record;
}
$record[$configuration['to']] = [
'lat' => (float) $record[$configuration['fields']['lat']],
'lon' => (float) $record[$configuration['fields']['lon']],
'lat' => (float) $record[$configuration['lat']],
'lon' => (float) $record[$configuration['lon']],
];
return $record;
}
protected function canApply(array $record, array $configuration) : bool
{
if (!isset($record[$configuration['lat']])
|| !is_numeric($record[$configuration['lat']])
|| trim($record[$configuration['lat']]) === ''
) {
return false;
}
if (!isset($record[$configuration['lon']])
|| !is_numeric($record[$configuration['lon']])
|| trim($record[$configuration['lon']]) === ''
) {
return false;
}
return true;
}
}

View file

@ -0,0 +1,23 @@
``Codappix\SearchCore\DataProcessing\CopyToProcessor``
======================================================
Will copy contents of fields to other fields.
Possible Options:
``to``
Defines the field to copy the values into. All values not false will be copied at the moment.
Example::
plugin.tx_searchcore.settings.indexing.tt_content.dataProcessing {
1 = Codappix\SearchCore\DataProcessing\CopyToProcessor
1 {
to = all
}
2 = Codappix\SearchCore\DataProcessing\CopyToProcessor
2 {
to = spellcheck
}
}

View file

@ -0,0 +1,27 @@
``Codappix\SearchCore\DataProcessing\GeoPointProcessor``
========================================================
Will create a new field, ready to use as GeoPoint field for Elasticsearch.
Possible Options:
``to``
Defines the field to create as GeoPoint.
``lat``
Defines the field containing the latitude.
``lon``
Defines the field containing the longitude.
Example::
plugin.tx_searchcore.settings.indexing.tt_content.dataProcessing {
1 = Codappix\SearchCore\DataProcessing\GeoPointProcessor
1 {
to = location
lat = lat
lon = lng
}
}
The above example will create a new field ``location`` as GeoPoint with latitude fetched from field
``lat`` and longitude fetched from field ``lng``.

View file

@ -175,8 +175,12 @@ dataProcessing
The following Processor are available:
``Codappix\SearchCore\DataProcessing\CopyToProcessor``
Will copy contents of fields to other fields
.. toctree::
:maxdepth: 1
:glob:
dataProcessing/CopyToProcessor
dataProcessing/GeoPointProcessor
The following Processor are planned:

View file

@ -0,0 +1,113 @@
<?php
namespace Codappix\SearchCore\Tests\Unit\DataProcessing;
/*
* Copyright (C) 2017 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Codappix\SearchCore\DataProcessing\GeoPointProcessor;
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
class GeoPointProcessorTest extends AbstractUnitTestCase
{
/**
* @test
* @dataProvider getPossibleRecordConfigurationCombinations
*/
public function geoPointsAreAddedAsConfigured(array $record, array $configuration, array $expectedRecord)
{
$subject = new GeoPointProcessor();
$processedRecord = $subject->processRecord($record, $configuration);
$this->assertSame(
$expectedRecord,
$processedRecord,
'The processor did not return the expected processed record.'
);
}
/**
* @return array
*/
public function getPossibleRecordConfigurationCombinations()
{
return [
'Create new field with existing lat and lng' => [
'record' => [
'lat' => 23.232,
'lng' => 45.43,
],
'configuration' => [
'to' => 'location',
'lat' => 'lat',
'lon' => 'lng',
],
'expectedRecord' => [
'lat' => 23.232,
'lng' => 45.43,
'location' => [
'lat' => 23.232,
'lon' => 45.43,
],
],
],
'Do not create new field due to missing configuration' => [
'record' => [
'lat' => 23.232,
'lng' => 45.43,
],
'configuration' => [
'to' => 'location',
],
'expectedRecord' => [
'lat' => 23.232,
'lng' => 45.43,
],
],
'Do not create new field due to missing lat and lon' => [
'record' => [
'lat' => '',
'lng' => '',
],
'configuration' => [
'to' => 'location',
'lat' => 'lat',
'lon' => 'lng',
],
'expectedRecord' => [
'lat' => '',
'lng' => '',
],
],
'Do not create new field due to invalid lat and lon' => [
'record' => [
'lat' => 'av',
'lng' => 'dsf',
],
'configuration' => [
'to' => 'location',
'lat' => 'lat',
'lon' => 'lng',
],
'expectedRecord' => [
'lat' => 'av',
'lng' => 'dsf',
],
],
];
}
}