mirror of
https://github.com/Codappix/search_core.git
synced 2024-11-23 20:16:10 +01:00
FEATURE: Allow to limit fields to copy in processor
Add configuration option to copy only some fields via CopyToProcessor. Also add missing test for CopyToProcessor.
This commit is contained in:
parent
a4179b4053
commit
cc275d1f32
2 changed files with 132 additions and 4 deletions
|
@ -20,18 +20,30 @@ namespace Codappix\SearchCore\DataProcessing;
|
||||||
* 02110-1301, USA.
|
* 02110-1301, USA.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies values from one field to another one.
|
* Copies values from one field to another one.
|
||||||
*/
|
*/
|
||||||
class CopyToProcessor implements ProcessorInterface
|
class CopyToProcessor implements ProcessorInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $keysToCopy = [];
|
||||||
|
|
||||||
public function processRecord(array $record, array $configuration)
|
public function processRecord(array $record, array $configuration)
|
||||||
{
|
{
|
||||||
$all = [];
|
$result = [];
|
||||||
|
$this->keysToCopy = array_keys($record);
|
||||||
|
|
||||||
$this->addArray($all, $record);
|
if (isset($configuration['from'])) {
|
||||||
$all = array_filter($all);
|
$this->keysToCopy = GeneralUtility::trimExplode(',', $configuration['from']);
|
||||||
$record[$configuration['to']] = implode(PHP_EOL, $all);
|
}
|
||||||
|
|
||||||
|
$this->addArray($result, $record);
|
||||||
|
$result = array_filter($result);
|
||||||
|
$record[$configuration['to']] = implode(PHP_EOL, $result);
|
||||||
|
|
||||||
return $record;
|
return $record;
|
||||||
}
|
}
|
||||||
|
@ -43,6 +55,9 @@ class CopyToProcessor implements ProcessorInterface
|
||||||
protected function addArray(array &$to, array $from)
|
protected function addArray(array &$to, array $from)
|
||||||
{
|
{
|
||||||
foreach ($from as $property => $value) {
|
foreach ($from as $property => $value) {
|
||||||
|
if (!in_array($property, $this->keysToCopy)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$this->addArray($to, $value);
|
$this->addArray($to, $value);
|
||||||
continue;
|
continue;
|
||||||
|
|
113
Tests/Unit/DataProcessing/CopyToProcessorTest.php
Normal file
113
Tests/Unit/DataProcessing/CopyToProcessorTest.php
Normal 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\CopyToProcessor;
|
||||||
|
use Codappix\SearchCore\Tests\Unit\AbstractUnitTestCase;
|
||||||
|
|
||||||
|
class CopyToProcessorTest extends AbstractUnitTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param array $record
|
||||||
|
* @param array $configuration
|
||||||
|
* @param array $expectedRecord
|
||||||
|
*
|
||||||
|
* @test
|
||||||
|
* @dataProvider getTestData
|
||||||
|
*/
|
||||||
|
public function recordIsReturnedExpectedtoProvidedConfiguration(array $record, array $configuration, array $expectedRecord)
|
||||||
|
{
|
||||||
|
$processor = new CopyToProcessor();
|
||||||
|
|
||||||
|
$this->assertSame(
|
||||||
|
$expectedRecord,
|
||||||
|
$processor->processRecord($record, $configuration),
|
||||||
|
''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTestData()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'copy all' => [
|
||||||
|
'record' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
],
|
||||||
|
'configuration' => [
|
||||||
|
'to' => 'new field',
|
||||||
|
],
|
||||||
|
'expectedRecord' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
'new field' => "Some content\nAnother content",
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'copy one' => [
|
||||||
|
'record' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
],
|
||||||
|
'configuration' => [
|
||||||
|
'from' => 'field_1',
|
||||||
|
'to' => 'new field',
|
||||||
|
],
|
||||||
|
'expectedRecord' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
'new field' => 'Some content',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'copy some' => [
|
||||||
|
'record' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
'field_3' => 'Another one',
|
||||||
|
],
|
||||||
|
'configuration' => [
|
||||||
|
'from' => 'field_1, field_3',
|
||||||
|
'to' => 'new field',
|
||||||
|
],
|
||||||
|
'expectedRecord' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
'field_3' => 'Another one',
|
||||||
|
'new field' => "Some content\nAnother one",
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'configure to copy non existing fields' => [
|
||||||
|
'record' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
],
|
||||||
|
'configuration' => [
|
||||||
|
'from' => 'non_existing, field_1',
|
||||||
|
'to' => 'new field',
|
||||||
|
],
|
||||||
|
'expectedRecord' => [
|
||||||
|
'field_1' => 'Some content',
|
||||||
|
'field_2' => 'Another content',
|
||||||
|
'new field' => 'Some content',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue