Skip to content

Commit

Permalink
Add bytes processor (#2008)
Browse files Browse the repository at this point in the history
* Add bytes processor

* Add changelog

* Add link to official documentation
  • Loading branch information
deguif authored Oct 29, 2021
1 parent f605ac5 commit 9bdb007
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added `ignore_failure` option to suitable ingest processors [#2003](https://github.com/ruflin/Elastica/pull/2003)
* Added `ignore_missing` option to `lowercase`, `remove`, `trim` and `uppercase` processors [#2001](https://github.com/ruflin/Elastica/pull/2001)
* Added `allow_duplicates` option to `append` processor [#2004](https://github.com/ruflin/Elastica/pull/2004)
* Added `bytes` processor [#2008](https://github.com/ruflin/Elastica/pull/2008)
### Changed
### Deprecated
### Removed
Expand Down
37 changes: 37 additions & 0 deletions src/Processor/BytesProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Elastica\Processor;

/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/bytes-processor.html
*/
class BytesProcessor extends AbstractProcessor
{
use Traits\IgnoreFailureTrait;
use Traits\IgnoreMissingTrait;

public function __construct(string $field)
{
$this->setField($field);
}

/**
* Set field.
*
* @return $this
*/
public function setField(string $field): self
{
return $this->setParam('field', $field);
}

/**
* Set target_field.
*
* @return $this
*/
public function setTargetField(string $targetField): self
{
return $this->setParam('target_field', $targetField);
}
}
68 changes: 68 additions & 0 deletions tests/Processor/BytesProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Elastica\Test\Processor;

use Elastica\Processor\BytesProcessor;
use Elastica\Test\BasePipeline as BasePipelineTest;

/**
* @internal
*/
class BytesProcessorTest extends BasePipelineTest
{
/**
* @group unit
*/
public function testBytes(): void
{
$processor = new BytesProcessor('foo');

$expected = [
'bytes' => [
'field' => 'foo',
],
];

$this->assertEquals($expected, $processor->toArray());
}

/**
* @group unit
*/
public function testBytesWithTargetField(): void
{
$processor = (new BytesProcessor('foo'))
->setTargetField('bar')
;

$expected = [
'bytes' => [
'field' => 'foo',
'target_field' => 'bar',
],
];

$this->assertEquals($expected, $processor->toArray());
}

/**
* @group unit
*/
public function testBytesWithNonDefaultOptions(): void
{
$processor = (new BytesProcessor('foo'))
->setIgnoreFailure(true)
->setIgnoreMissing(true)
;

$expected = [
'bytes' => [
'field' => 'foo',
'ignore_failure' => true,
'ignore_missing' => true,
],
];

$this->assertEquals($expected, $processor->toArray());
}
}

0 comments on commit 9bdb007

Please sign in to comment.