Encoding::keywordField()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 3
rs 9.9666
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Helper;
4
5
final class Encoding
6
{
7
    /**
8
     * The maximum number of characters that are accepted in a keyword field.
9
     */
10
    const KEYWORD_MAX_LENGTH = 1024;
11
12
    /**
13
     * Limit the size of keyword fields.
14
     *
15
     * @param int|string|null $value
16
     *
17
     * @return string
18
     */
19 40
    public static function keywordField($value)
20
    {
21 40
        if (false === is_string($value)) {
22 4
            return $value;
23
        }
24
25 40
        if (strlen($value) <= self::KEYWORD_MAX_LENGTH) {
26 39
            return $value;
27
        }
28
29 1
        $value = mb_substr(
30 1
            $value,
31 1
            0,
32 1
            self::KEYWORD_MAX_LENGTH - 1,
33
            'UTF-8'
34 1
        );
35
36 1
        return $value . '…';
37
    }
38
}
39