Passed
Push — master ( 63f627...202fbf )
by Adrien
14:30
created

Format::splitSearchTerms()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 30
ccs 0
cts 0
cp 0
rs 9.8333
cc 4
nc 3
nop 1
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ecodev\Felix;
6
7
use Money\Currencies\ISOCurrencies;
8
use Money\Formatter\DecimalMoneyFormatter;
9
use Money\Money;
10
use Normalizer;
11
12
abstract class Format
13
{
14
    private const PUNCTUATIONS = [
15
        '.', '।', '։', '。', '۔', '⳹', '܁', '።', '᙮', '᠃', '⳾', '꓿', '꘎', '꛳', '࠽', '᭟', ',', '،', '、', '՝', '߸', '፣',
16
        '᠈', '꓾', '꘍', '꛵', '᭞', '⁇', '⁉', '⁈', '‽', '❗', '‼', '⸘', '?', ';', '¿', '؟', '՞', '܆', '፧', '⳺', '⳻', '꘏',
17
        '꛷', '𑅃', '꫱', '!', '¡', '߹', '᥄', '·', '𐎟', '𐏐', '𒑰', '፡', ' ', '𐤟', '࠰', '—', '–', '‒', '‐', '⁃', '﹣', '-',
18
        '֊', '᠆', ';', '·', '؛', '፤', '꛶', '․', ':', '፥', '꛴', '᭝', '…', '︙', 'ຯ', '«', '‹', '»', '›', '„', '‚', '“',
19
        '‟', '‘', '‛', '”', '’', '"', "'", '(', ')',
20 5
    ];
21
22 5
    /**
23 3
     * Truncate a string and append '…' at the end.
24 3
     *
25
     * @param string $ellipsis the string to indicate truncation happened
26
     *
27 5
     * @return string truncated string
28
     */
29
    public static function truncate(string $string, int $maxLength, string $ellipsis = '…'): string
30
    {
31
        if (mb_strlen($string) > $maxLength) {
32
            $string = mb_substr($string, 0, $maxLength - mb_strlen($ellipsis));
33
            $string .= $ellipsis;
34
        }
35
36
        return $string;
37
    }
38
39
    /**
40
     * Shortcut to format money.
41
     */
42
    public static function money(Money $money): string
43
    {
44
        $currencies = new ISOCurrencies();
45
        $moneyFormatter = new DecimalMoneyFormatter($currencies);
46
47
        return $moneyFormatter->format($money);
48
    }
49
50
    /**
51
     * Parse the term to extract a list of words and quoted terms.
52
     *
53
     * @return string[]
54
     */
55
    public static function splitSearchTerms(?string $term): array
56
    {
57
        if (!$term) {
58
            return [];
59
        }
60
61
        /** @var string $term */
62
        $term = Normalizer::normalize($term);
63
64
        // Drop empty quote
65
        $term = str_replace('""', '', $term);
66
67
        // Extract exact terms that are quoted
68
        preg_match_all('~"([^"]*)"~', $term, $m);
69
        $exactTerms = $m[1];
70
        $termWithoutExact = str_replace($m[0], ' ', $term);
71
        $termWithoutExactWithoutPunctuations = str_replace(self::PUNCTUATIONS, ' ', $termWithoutExact);
72
73
        // Split words by any whitespace
74
        $words = preg_split('/[[:space:]]+/', $termWithoutExactWithoutPunctuations, -1, PREG_SPLIT_NO_EMPTY) ?: [];
75
76
        // Combine both list
77
        if ($exactTerms) {
78
            array_push($words, ...$exactTerms);
79
        }
80
81
        // Drop duplicates
82
        $words = array_unique($words);
83
84
        return $words;
85
    }
86
}
87