StringUtils::commonPrefix()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 2
nop 2
crap 4
1
<?php
2
/*
3
* The MIT License (MIT)
4
* Copyright © 2017 Marcos Lois Bermudez <[email protected]>
5
* *
6
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
* *
11
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
* of the Software.
13
* *
14
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
* DEALINGS IN THE SOFTWARE.
19
*/
20
21
namespace TTIC\Commons;
22
23
/**
24
 * Common string utility class.
25
 * Some of them based on source code from:
26
 * http://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php#answer-834355
27
 *
28
 * @package TTIC
29
 * @author Marcos Lois Bermúdez <[email protected]>
30
 */
31
abstract class StringUtils
32
{
33
    /**
34
     * Check if a string has a prefix.
35
     *
36
     * @param $haystack string The input string.
37
     * @param $needle string The string to search.
38
     * @param bool $caseInsensitive If string comparision is made case insensitive.
39
     * @return bool true if the needle if found, false otherwise
40
     */
41 3
    public static function startsWith($haystack, $needle, $caseInsensitive = false)
42
    {
43 3
        $length = strlen($needle);
44 3
        if ($length == 0) {
45 3
            return true;
46
        }
47
48 3
        return ($caseInsensitive ? (strcasecmp(substr($haystack, 0, $length), $needle) == 0)
49 3
            : (substr($haystack, 0, $length) === $needle));
50
    }
51
52
    /**
53
     * Check if a string has a suffix.
54
     *
55
     * @param $haystack string The input string.
56
     * @param $needle string The string to search.
57
     * @param bool $caseInsensitive If string comparision is made case insensitive.
58
     * @return bool true if the needle if found, false otherwise
59
     */
60 3
    public static function endsWith($haystack, $needle, $caseInsensitive = false)
61
    {
62 3
        $length = strlen($needle);
63 3
        if ($length == 0) {
64 3
            return true;
65
        }
66
67 3
        return ($caseInsensitive ? (strcasecmp(substr($haystack, -$length), $needle) == 0)
68 3
            : (substr($haystack, -$length) === $needle));
69
    }
70
71
    /**
72
     * Get the common prefix of a list of strings.
73
     *
74
     * @param array $strings List of string to get prefix of.
75
     * @param bool $caseInsensitive If string comparision is made case insensitive.
76
     * @return string The common prefix to all strings or a empty string.
77
     */
78 3
    public static function commonPrefix(array $strings, $caseInsensitive = false)
79
    {
80 3
        sort($strings);
81
82 3
        $s1 = array_shift($strings);
83 3
        $s2 = array_pop($strings);
84 3
        $len = min(strlen($s1), strlen($s2));
85
86 3
        for ($i = 0; $i < $len && ($caseInsensitive ? strcasecmp($s1[$i], $s2[$i]) == 0 : $s1[$i] === $s2[$i]); $i++);
87
88 3
        return substr($s1, 0, $i);
89
    }
90
}
91