From 3a7fa8b5f1556d52aae0343e5a0f46f181724d75 Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Mon, 19 Oct 2020 16:57:34 +0200 Subject: [PATCH 01/10] Add Foreach processor --- src/Processor/Foreach.php | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/Processor/Foreach.php diff --git a/src/Processor/Foreach.php b/src/Processor/Foreach.php new file mode 100644 index 0000000000..f89ec71f48 --- /dev/null +++ b/src/Processor/Foreach.php @@ -0,0 +1,69 @@ + + * @author Thibaut Simon-Fine + * + * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/foreach-processor.html + */ +class ForeachProcessor extends AbstractProcessor +{ + public const DEFAULT_IGNORE_MISSING_VALUE = false; + + /** + * Foreach constructor. + */ + public function __construct(string $field) + { + $this->setField($field); + } + + /** + * Set field. + * + * @return $this + */ + public function setField(string $field): self + { + return $this->setParam('field', $field); + } + + /** + * Set processor. + * + * @param AbstractProcessor + * + * @return $this + */ + public function setProcessor(AbstractProcessor $processor): self + { + return $this->setParam('processor', [$processor]); + } + + /** + * Set raw processor. + * Example : ['remove' => ['field' => 'user_agent']] + * + * @return $this + */ + public function setRawProcessor(array $processor): self + { + return $this->setParam('processor', $processor); + } + + /** + * Set ignore_missing. Default value false. + * + * If true and field does not exist or is null, the processor quietly exits without modifying the document + * + * @return $this + */ + public function setIgnoreMissing(bool $ignoreMissing): self + { + return $this->setParam('ignore_missing', $ignoreMissing); + } +} From 352799189d862d28acad045f89009341b2bc902d Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Mon, 19 Oct 2020 17:03:39 +0200 Subject: [PATCH 02/10] Rename Foreach.php in order to match with class name --- src/Processor/{Foreach.php => ForeachProcessor.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Processor/{Foreach.php => ForeachProcessor.php} (100%) diff --git a/src/Processor/Foreach.php b/src/Processor/ForeachProcessor.php similarity index 100% rename from src/Processor/Foreach.php rename to src/Processor/ForeachProcessor.php From 630c30b62289beb912c1f068eab16f21e40b8f0e Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Mon, 19 Oct 2020 17:29:43 +0200 Subject: [PATCH 03/10] Cleanup --- src/Processor/ForeachProcessor.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Processor/ForeachProcessor.php b/src/Processor/ForeachProcessor.php index f89ec71f48..3f0df38283 100644 --- a/src/Processor/ForeachProcessor.php +++ b/src/Processor/ForeachProcessor.php @@ -34,7 +34,7 @@ public function setField(string $field): self /** * Set processor. - * + * * @param AbstractProcessor * * @return $this @@ -46,7 +46,7 @@ public function setProcessor(AbstractProcessor $processor): self /** * Set raw processor. - * Example : ['remove' => ['field' => 'user_agent']] + * Example : ['remove' => ['field' => 'user_agent']]. * * @return $this */ @@ -59,7 +59,7 @@ public function setRawProcessor(array $processor): self * Set ignore_missing. Default value false. * * If true and field does not exist or is null, the processor quietly exits without modifying the document - * + * * @return $this */ public function setIgnoreMissing(bool $ignoreMissing): self From 1bebd01ece7acaf59b3dd5673b8471b83df29ad4 Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Mon, 19 Oct 2020 17:31:02 +0200 Subject: [PATCH 04/10] Add Foreach processor test file --- tests/Processor/ForeachProcessorTest.php | 131 +++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tests/Processor/ForeachProcessorTest.php diff --git a/tests/Processor/ForeachProcessorTest.php b/tests/Processor/ForeachProcessorTest.php new file mode 100644 index 0000000000..1001cd18db --- /dev/null +++ b/tests/Processor/ForeachProcessorTest.php @@ -0,0 +1,131 @@ +setProcessor($subprocessor); + + $expected = [ + 'foreach' => [ + 'field' => 'field1', + 'processor' => [ + 'uppercase' => [ + 'field' => 'field2', + ], + ], + ], + ]; + + $this->assertEquals($expected, $processor->toArray()); + } + + /** + * @group unit + */ + public function testForeachRawProcessorDefault(): void + { + $processor = new ForeachProcessor('field1'); + $subprocessor = [ + 'processor' => [ + 'uppercase' => [ + 'field' => 'field2', + ], + ], + ]; + $processor->setRawProcessor($subprocessor); + + $expected = [ + 'foreach' => [ + 'field' => 'field1', + 'processor' => [ + 'uppercase' => [ + 'field' => 'field2', + ], + ], + ], + ]; + + $this->assertEquals($expected, $processor->toArray()); + } + + /** + * @group unit + */ + public function testForeachProcessorIgnoreMissing(): void + { + $processor = new ForeachProcessor('field1'); + $subprocessor = new Uppercase('field2'); + $processor->setProcessor($subprocessor); + $processor->setIgnoreMissing(true); + + $expected = [ + 'foreach' => [ + 'field' => 'field1', + 'processor' => [ + 'uppercase' => [ + 'field' => 'field2', + ], + ], + 'ignore_missing' => true, + ], + ]; + + $this->assertEquals($expected, $processor->toArray()); + } + + /** + * @group functional + */ + public function testForeachProcessor(): void + { + $foreach = new ForeachProcessor('values'); + $subprocessor = new Uppercase('_ingest._value'); + $foreach->setProcessor($subprocessor); + + $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Foreach'); + $pipeline->addProcessor($foreach)->create(); + + $index = $this->_createIndex(); + $bulk = new Bulk($index->getClient()); + $bulk->setIndex($index); + + $bulk->addDocuments([ + new Document(null, ['name' => 'ruflin', 'type' => 'elastica', 'values' => ['foo', 'bar', 'baz']]), + ]); + $bulk->setRequestParam('pipeline', 'my_custom_pipeline'); + + $bulk->send(); + $index->refresh(); + + /** @var ResultSet $result */ + $result = $index->search('*'); + + $this->assertCount(1, $result->getResults()); + + foreach ($result->getResults() as $rx) { + $value = $rx->getData(); + $this->assertCount(3, $value['values']); + $this->assertEquals('FOO', $value['values'][0]); + $this->assertEquals('BAR', $value['values'][1]); + $this->assertEquals('BAZ', $value['values'][2]); + } + } +} From ea076d6e556558d2d96497023ec72f1ca2d1aef4 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Mon, 19 Oct 2020 19:07:06 +0200 Subject: [PATCH 05/10] Update src/Processor/ForeachProcessor.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François-Xavier de Guillebon --- src/Processor/ForeachProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Processor/ForeachProcessor.php b/src/Processor/ForeachProcessor.php index 3f0df38283..52b6b36991 100644 --- a/src/Processor/ForeachProcessor.php +++ b/src/Processor/ForeachProcessor.php @@ -3,7 +3,7 @@ namespace Elastica\Processor; /** - * Elastica Attachment Processor. + * Elastica Foreach Processor. * * @author Federico Panini * @author Thibaut Simon-Fine From 0245b070cea7a35e59718d91f2785ad6de02ecf9 Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Tue, 20 Oct 2020 09:58:54 +0200 Subject: [PATCH 06/10] Code fixes - [Remove constructor](https://github.com/ruflin/Elastica/pull/1813#discussion_r507872425) - Fix processor name being "foreach_processor" instead of "processor" due to class name (note: 'Foreach' is reserved) - Fix ForeachProcessor.setProcessor method - Update ForeachProcessorTest, all tests runs locally --- src/Processor/ForeachProcessor.php | 23 ++++++++++++++--------- tests/Processor/ForeachProcessorTest.php | 22 ++++++++++++++-------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Processor/ForeachProcessor.php b/src/Processor/ForeachProcessor.php index 52b6b36991..5e09aa27ac 100644 --- a/src/Processor/ForeachProcessor.php +++ b/src/Processor/ForeachProcessor.php @@ -13,14 +13,7 @@ class ForeachProcessor extends AbstractProcessor { public const DEFAULT_IGNORE_MISSING_VALUE = false; - - /** - * Foreach constructor. - */ - public function __construct(string $field) - { - $this->setField($field); - } + protected const PROCESSOR_NAME = 'foreach'; /** * Set field. @@ -41,7 +34,7 @@ public function setField(string $field): self */ public function setProcessor(AbstractProcessor $processor): self { - return $this->setParam('processor', [$processor]); + return $this->setParam('processor', $processor); } /** @@ -66,4 +59,16 @@ public function setIgnoreMissing(bool $ignoreMissing): self { return $this->setParam('ignore_missing', $ignoreMissing); } + + /** + * Param's name + * Picks the last part of the class name and makes it snake_case + * You can override this method if you want to change the name. + * + * @return string name + */ + protected function _getBaseName() + { + return self::PROCESSOR_NAME; + } } diff --git a/tests/Processor/ForeachProcessorTest.php b/tests/Processor/ForeachProcessorTest.php index 1001cd18db..bcea913600 100644 --- a/tests/Processor/ForeachProcessorTest.php +++ b/tests/Processor/ForeachProcessorTest.php @@ -19,7 +19,9 @@ class ForeachProcessorTest extends BasePipelineTest */ public function testForeachProcessorDefault(): void { - $processor = new ForeachProcessor('field1'); + $processor = new ForeachProcessor(); + $processor->setField('field1'); + $subprocessor = new Uppercase('field2'); $processor->setProcessor($subprocessor); @@ -42,12 +44,12 @@ public function testForeachProcessorDefault(): void */ public function testForeachRawProcessorDefault(): void { - $processor = new ForeachProcessor('field1'); + $processor = new ForeachProcessor(); + $processor->setField('field1'); + $subprocessor = [ - 'processor' => [ - 'uppercase' => [ - 'field' => 'field2', - ], + 'uppercase' => [ + 'field' => 'field2', ], ]; $processor->setRawProcessor($subprocessor); @@ -71,7 +73,9 @@ public function testForeachRawProcessorDefault(): void */ public function testForeachProcessorIgnoreMissing(): void { - $processor = new ForeachProcessor('field1'); + $processor = new ForeachProcessor(); + $processor->setField('field1'); + $subprocessor = new Uppercase('field2'); $processor->setProcessor($subprocessor); $processor->setIgnoreMissing(true); @@ -96,7 +100,9 @@ public function testForeachProcessorIgnoreMissing(): void */ public function testForeachProcessor(): void { - $foreach = new ForeachProcessor('values'); + $foreach = new ForeachProcessor(); + $foreach->setField('values'); + $subprocessor = new Uppercase('_ingest._value'); $foreach->setProcessor($subprocessor); From e90288a4308054660eea24ebca5e1fb36a1ebc00 Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Tue, 20 Oct 2020 17:34:06 +0200 Subject: [PATCH 07/10] Add constructor with field and processor --- src/Processor/ForeachProcessor.php | 18 +++++++++++++++++- tests/Processor/ForeachProcessorTest.php | 20 ++++---------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Processor/ForeachProcessor.php b/src/Processor/ForeachProcessor.php index 5e09aa27ac..e12dc36d79 100644 --- a/src/Processor/ForeachProcessor.php +++ b/src/Processor/ForeachProcessor.php @@ -15,6 +15,22 @@ class ForeachProcessor extends AbstractProcessor public const DEFAULT_IGNORE_MISSING_VALUE = false; protected const PROCESSOR_NAME = 'foreach'; + /** + * @param AbstractProcessor|array $processor + */ + public function __construct(string $field, $processor) + { + $this->setField($field); + + if ($processor instanceof AbstractProcessor) { + $this->setProcessor($processor); + } elseif (\is_array($processor)) { + $this->setRawProcessor($processor); + } else { + throw new \TypeError('Expected Elastica\Processor\AbstractProcessor or array'); + } + } + /** * Set field. * @@ -32,7 +48,7 @@ public function setField(string $field): self * * @return $this */ - public function setProcessor(AbstractProcessor $processor): self + public function setProcessor(?AbstractProcessor $processor): self { return $this->setParam('processor', $processor); } diff --git a/tests/Processor/ForeachProcessorTest.php b/tests/Processor/ForeachProcessorTest.php index bcea913600..cc2de7850b 100644 --- a/tests/Processor/ForeachProcessorTest.php +++ b/tests/Processor/ForeachProcessorTest.php @@ -19,11 +19,8 @@ class ForeachProcessorTest extends BasePipelineTest */ public function testForeachProcessorDefault(): void { - $processor = new ForeachProcessor(); - $processor->setField('field1'); - $subprocessor = new Uppercase('field2'); - $processor->setProcessor($subprocessor); + $processor = new ForeachProcessor('field1', $subprocessor); $expected = [ 'foreach' => [ @@ -44,15 +41,12 @@ public function testForeachProcessorDefault(): void */ public function testForeachRawProcessorDefault(): void { - $processor = new ForeachProcessor(); - $processor->setField('field1'); - $subprocessor = [ 'uppercase' => [ 'field' => 'field2', ], ]; - $processor->setRawProcessor($subprocessor); + $processor = new ForeachProcessor('field1', $subprocessor); $expected = [ 'foreach' => [ @@ -73,11 +67,8 @@ public function testForeachRawProcessorDefault(): void */ public function testForeachProcessorIgnoreMissing(): void { - $processor = new ForeachProcessor(); - $processor->setField('field1'); - $subprocessor = new Uppercase('field2'); - $processor->setProcessor($subprocessor); + $processor = new ForeachProcessor('field1', $subprocessor); $processor->setIgnoreMissing(true); $expected = [ @@ -100,11 +91,8 @@ public function testForeachProcessorIgnoreMissing(): void */ public function testForeachProcessor(): void { - $foreach = new ForeachProcessor(); - $foreach->setField('values'); - $subprocessor = new Uppercase('_ingest._value'); - $foreach->setProcessor($subprocessor); + $foreach = new ForeachProcessor('values', $subprocessor); $pipeline = $this->_createPipeline('my_custom_pipeline', 'pipeline for Foreach'); $pipeline->addProcessor($foreach)->create(); From fcd3caa56998c93d064da2035f75db3a04c1010b Mon Sep 17 00:00:00 2001 From: Thibaut Date: Wed, 21 Oct 2020 09:19:51 +0200 Subject: [PATCH 08/10] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François-Xavier de Guillebon --- src/Processor/ForeachProcessor.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Processor/ForeachProcessor.php b/src/Processor/ForeachProcessor.php index e12dc36d79..cbb539bc62 100644 --- a/src/Processor/ForeachProcessor.php +++ b/src/Processor/ForeachProcessor.php @@ -27,7 +27,8 @@ public function __construct(string $field, $processor) } elseif (\is_array($processor)) { $this->setRawProcessor($processor); } else { - throw new \TypeError('Expected Elastica\Processor\AbstractProcessor or array'); + throw new \TypeError(\sprintf('Argument 2 passed to %s::__construct() must be of type %s|array, %s given.', self::class, AbstractProcessor::class, \is_object($processor) ? \get_class($processor) : \gettype($processor))); + } } @@ -48,7 +49,7 @@ public function setField(string $field): self * * @return $this */ - public function setProcessor(?AbstractProcessor $processor): self + public function setProcessor(AbstractProcessor $processor): self { return $this->setParam('processor', $processor); } From d275e1ad848e233be808a2d967cfa6f88118a778 Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Mon, 7 Dec 2020 14:25:48 +0100 Subject: [PATCH 09/10] Apply #1892 modifications & Coding style CI - Remove _getBaseName() override - Remove an empty line in constructor --- src/Processor/ForeachProcessor.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/Processor/ForeachProcessor.php b/src/Processor/ForeachProcessor.php index cbb539bc62..49c7dab64c 100644 --- a/src/Processor/ForeachProcessor.php +++ b/src/Processor/ForeachProcessor.php @@ -13,7 +13,6 @@ class ForeachProcessor extends AbstractProcessor { public const DEFAULT_IGNORE_MISSING_VALUE = false; - protected const PROCESSOR_NAME = 'foreach'; /** * @param AbstractProcessor|array $processor @@ -28,7 +27,6 @@ public function __construct(string $field, $processor) $this->setRawProcessor($processor); } else { throw new \TypeError(\sprintf('Argument 2 passed to %s::__construct() must be of type %s|array, %s given.', self::class, AbstractProcessor::class, \is_object($processor) ? \get_class($processor) : \gettype($processor))); - } } @@ -76,16 +74,4 @@ public function setIgnoreMissing(bool $ignoreMissing): self { return $this->setParam('ignore_missing', $ignoreMissing); } - - /** - * Param's name - * Picks the last part of the class name and makes it snake_case - * You can override this method if you want to change the name. - * - * @return string name - */ - protected function _getBaseName() - { - return self::PROCESSOR_NAME; - } } From 29d2c6f12a92a18718ebb65de5cc59496c283309 Mon Sep 17 00:00:00 2001 From: ThibautSF Date: Mon, 7 Dec 2020 15:52:26 +0100 Subject: [PATCH 10/10] Apply last review --- src/Processor/ForeachProcessor.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Processor/ForeachProcessor.php b/src/Processor/ForeachProcessor.php index 49c7dab64c..f62881e03a 100644 --- a/src/Processor/ForeachProcessor.php +++ b/src/Processor/ForeachProcessor.php @@ -43,8 +43,6 @@ public function setField(string $field): self /** * Set processor. * - * @param AbstractProcessor - * * @return $this */ public function setProcessor(AbstractProcessor $processor): self