This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from FreezeWarp/master
Add handling for keyword fields with a maximum length of 1024 characters
- Loading branch information
Showing
7 changed files
with
100 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace PhilKra\Helper; | ||
|
||
/* | ||
* Functions to convert values for transmission to ElasticSearch. | ||
*/ | ||
class Encoding | ||
{ | ||
|
||
/** | ||
* The maximum number of characters that are accepted in a keyword field. | ||
*/ | ||
const KEYWORD_MAX_LENGTH = 1024; | ||
|
||
|
||
/** | ||
* Limit the size of keyword fields. This is the same approach used by the Python APM client. | ||
* | ||
* @param string $value | ||
* @return string | ||
*/ | ||
public static function keywordField($value) | ||
{ | ||
if (strlen($value) > self::KEYWORD_MAX_LENGTH && mb_strlen($value, 'UTF-8') > self::KEYWORD_MAX_LENGTH) { // strlen is faster (O(1)), so we prefer to first check using it, and then double-checking with the slower mb_strlen (O(n)) only when necessary | ||
return mb_substr($value, 0, self::KEYWORD_MAX_LENGTH - 1, 'UTF-8') . '…'; | ||
} | ||
|
||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
namespace PhilKra\Tests\Helper; | ||
|
||
use \PhilKra\Helper\Encoding; | ||
use PhilKra\Tests\TestCase; | ||
|
||
/** | ||
* Test Case for @see \PhilKra\Helper\Encoding | ||
*/ | ||
final class EncodingTest extends TestCase { | ||
|
||
/** | ||
* @covers \PhilKra\Helper\Encoding::keywordField | ||
*/ | ||
public function testShortInput() { | ||
|
||
$input = "abcdefghijklmnopqrstuvwxyz1234567890"; | ||
|
||
$this->assertEquals( $input, Encoding::keywordField($input) ); | ||
|
||
} | ||
|
||
/** | ||
* @covers \PhilKra\Helper\Encoding::keywordField | ||
*/ | ||
public function testLongInput() { | ||
|
||
$input = str_repeat("abc123", 200); | ||
$output = str_repeat("abc123", 170) . 'abc' . '…'; | ||
|
||
$this->assertEquals( $output, Encoding::keywordField($input) ); | ||
|
||
} | ||
|
||
/** | ||
* @covers \PhilKra\Helper\Encoding::keywordField | ||
*/ | ||
public function testLongMultibyteInput() { | ||
|
||
$input = str_repeat("中国日本韓国合衆国", 200); | ||
$output = str_repeat("中国日本韓国合衆国", 113) . '中国日本韓国' . '…'; | ||
|
||
$this->assertEquals( $output, Encoding::keywordField($input) ); | ||
|
||
} | ||
|
||
} |