StringSupport   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 104
ccs 26
cts 30
cp 0.8667
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parametrize() 0 11 1
A match() 0 4 1
A find() 0 9 2
A findOne() 0 14 3
B findFew() 0 24 4
1
<?php
2
3
namespace Dazzle\Util\Support;
4
5
abstract class StringSupport
6
{
7
    /**
8
     * Parametrize string using array of params.
9
     *
10
     * @param string $str
11
     * @param string[] $params
12
     * @return string
13
     */
14 1
    public static function parametrize($str, $params)
15
    {
16 1
        $keys = array_keys($params);
17 1
        $vals = array_values($params);
18
19 1
        array_walk($keys, function(&$key) {
20 1
            $key = '%' . $key . '%';
21 1
        });
22
23 1
        return str_replace($keys, $vals, $str);
24
    }
25
26
    /**
27
     * Check if string matches pattern using simple regexp method.
28
     *
29
     * @param string $pattern
30
     * @param string $string
31
     * @return bool
32
     */
33 5
    public static function match($pattern, $string)
34
    {
35 5
        return fnmatch($pattern, $string);
36
    }
37
38
    /**
39
     * Filter and return entries that match any of specified patterns.
40
     *
41
     * @param string|string[] $pattern
42
     * @param string[] $entries
43
     * @return string[]
44
     */
45
    public static function find($pattern, $entries)
46
    {
47
        if (is_array($pattern))
48
        {
49
            return static::findFew($pattern, $entries);
50
        }
51
52
        return static::findOne($pattern, $entries);
53
    }
54
55
    /**
56
     * Filter and return entries that match specified pattern.
57
     *
58
     * @param string $pattern
59
     * @param string[] $entries
60
     * @return string[]
61
     */
62 4
    public static function findOne($pattern, $entries)
63
    {
64 4
        $found = [];
65
66 4
        foreach ($entries as $entry)
67
        {
68 4
            if (static::match($pattern, $entry))
69
            {
70 4
                $found[] = $entry;
71
            }
72
        }
73
74 4
        return $found;
75
    }
76
77
    /**
78
     * Filter and return entries that match any of specified patterns.
79
     *
80
     * @param string[] $patterns
81
     * @param string[] $entries
82
     * @return string[]
83
     */
84 2
    public static function findFew($patterns, $entries)
85
    {
86 2
        $found = [];
87
88 2
        foreach ($patterns as $pattern)
89
        {
90 2
            $found = array_merge($found, static::findOne($pattern, $entries));
91
        }
92
93 2
        $found = array_unique($found);
94 2
        $results = [];
95
96 2
        foreach ($entries as $entry)
97
        {
98 2
            if (in_array($entry, $found, true) === true)
99
            {
100 2
                $results[] = $entry;
101
            }
102
        }
103
104 2
        unset($found);
105
106 2
        return $results;
107
    }
108
}
109