Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Pipeline processor handling #2218

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed

### Fixed
* Fixed Pipeline Processor handling to allow for multiple processors of the same type [#2218](https://github.com/ruflin/Elastica/pull/2218)

### Security

Expand Down
25 changes: 7 additions & 18 deletions src/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,10 @@ class Pipeline extends Param
*/
protected $_client;

/**
* @var AbstractProcessor[]
*
* @phpstan-var array{processors?: AbstractProcessor[]}
*/
protected $_processors = [];

public function __construct(Client $client)
{
$this->_client = $client;
$this->setParam('processors', []);
}

/**
Expand All @@ -55,7 +49,7 @@ public function create(): Response
throw new InvalidException('You should set a valid processor description.');
}

if (empty($this->_processors['processors'])) {
if (empty($this->_params['processors'])) {
throw new InvalidException('You should set a valid processor of type Elastica\Processor\AbstractProcessor.');
}

Expand Down Expand Up @@ -95,19 +89,14 @@ public function deletePipeline(string $id): Response
*/
public function setRawProcessors(array $processors): self
{
$this->_processors = $processors;
$this->setParam('processors', $processors);

return $this;
}

public function addProcessor(AbstractProcessor $processor): self
{
if (!$this->_processors) {
$this->_processors['processors'] = $processor->toArray();
$this->_params['processors'] = [];
} else {
$this->_processors['processors'] = \array_merge($this->_processors['processors'], $processor->toArray());
}
$this->setParam('processors', \array_merge($this->getParam('processors'), [$processor->toArray()]));

return $this;
}
Expand All @@ -129,7 +118,9 @@ public function getId(): ?string
*/
public function setProcessors(array $processors): self
{
$this->setParam('processors', [$processors]);
foreach ($processors as $processor) {
$this->addProcessor($processor);
}

return $this;
}
Expand All @@ -148,8 +139,6 @@ public function setDescription(string $description): self
*/
public function toArray(): array
{
$this->_params['processors'] = [$this->_processors['processors']];

return $this->getParams();
}

Expand Down
29 changes: 18 additions & 11 deletions tests/PipelineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,25 @@ public function testProcessor(): void

$expected = [
'description' => 'this is a new pipeline',
'processors' => [[
'trim' => [
'field' => 'field1',
'processors' => [
[
'trim' => [
'field' => 'field1',
],
],
'rename' => [
'field' => 'foo',
'target_field' => 'target.field',
[
'rename' => [
'field' => 'foo',
'target_field' => 'target.field',
],
],
'set' => [
'field' => 'field4',
'value' => 324,
[
'set' => [
'field' => 'field4',
'value' => 324,
],
],
]],
],
];

$this->assertEquals($expected, $processors->toArray());
Expand Down Expand Up @@ -79,7 +85,8 @@ public function testPipelineCreate(): void
$this->assertSame('pipeline for Set', $result['my_custom_pipeline']['description']);
$this->assertSame('field4', $result['my_custom_pipeline']['processors'][0]['set']['field']);
$this->assertSame(333, $result['my_custom_pipeline']['processors'][0]['set']['value']);
$this->assertSame('field1', $result['my_custom_pipeline']['processors'][0]['trim']['field']);
$this->assertSame('field1', $result['my_custom_pipeline']['processors'][1]['trim']['field']);
$this->assertSame('foo', $result['my_custom_pipeline']['processors'][2]['rename']['field']);
}

/**
Expand Down
Loading