Passed
Push — master ( 202fbf...ff93fc )
by Adrien
03:14
created

Format::splitSearchTerms()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 30
ccs 14
cts 14
cp 1
rs 9.8333
cc 4
nc 3
nop 1
crap 4
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
    ];
21
22
    /**
23
     * Truncate a string and append '…' at the end.
24
     *
25
     * @param string $ellipsis the string to indicate truncation happened
26
     *
27
     * @return string truncated string
28
     */
29 5
    public static function truncate(string $string, int $maxLength, string $ellipsis = '…'): string
30
    {
31 5
        if (mb_strlen($string) > $maxLength) {
32 3
            $string = mb_substr($string, 0, $maxLength - mb_strlen($ellipsis));
33 3
            $string .= $ellipsis;
34
        }
35
36 5
        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 32
    public static function splitSearchTerms(?string $term): array
56
    {
57 32
        if (!$term) {
58 3
            return [];
59
        }
60
61
        /** @var string $term */
62 29
        $term = Normalizer::normalize($term);
63
64
        // Drop empty quote
65 29
        $term = str_replace('""', '', $term);
66
67
        // Extract exact terms that are quoted
68 29
        preg_match_all('~"([^"]*)"~', $term, $m);
69 29
        $exactTerms = $m[1];
70 29
        $termWithoutExact = str_replace($m[0], ' ', $term);
71 29
        $termWithoutExactWithoutPunctuations = str_replace(self::PUNCTUATIONS, ' ', $termWithoutExact);
72
73
        // Split words by any whitespace
74 29
        $words = preg_split('/[[:space:]]+/', $termWithoutExactWithoutPunctuations, -1, PREG_SPLIT_NO_EMPTY) ?: [];
75
76
        // Combine both list
77 29
        if ($exactTerms) {
78 10
            array_push($words, ...$exactTerms);
79
        }
80
81
        // Drop duplicates
82 29
        $words = array_unique($words);
83
84 29
        return $words;
85
    }
86
}
87