Failed Conditions
Pull Request — master (#7)
by Chad
03:43
created

Strings::ellipsize()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 16
nc 7
nop 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A Strings::ucwords() 0 14 2
1
<?php
2
/**
3
 * Defines \TraderInteractive\Util\Strings class.
4
 */
5
6
namespace TraderInteractive\Util;
7
8
/**
9
 * Static class with various string functions.
10
 */
11
final class Strings
12
{
13
    /**
14
     * Replaces the format items in a specified string with the string representation of n specified objects.
15
     *
16
     * @param string $format A composit format string
17
     * @param mixed  $arguments Variable number of items to format.
18
     *
19
     * @return string Returns a copy of format in which the format items have been
20
     *     replaced by the string representations of arg0, arg1,... argN.
21
     */
22
    public static function format(string $format, string ...$arguments) : string
23
    {
24
        foreach ($arguments as $key => $value) {
25
            if (is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
26
                $format = str_replace("{{$key}}", (string)$value, $format);
27
                continue;
28
            }
29
        }
30
31
        return $format;
32
    }
33
34
    /**
35
     * Checks if $string ends with $suffix and puts the rest of the $string in $nonSuffix.
36
     *
37
     * @param string $string The string to check
38
     * @param string $suffix The suffix to check for
39
     * @param mixed &$nonSuffix This is the part of the string that is not the suffix.
40
     *
41
     * @return bool whether the $string ended with $suffix or not.
42
     */
43
    public static function endsWith(string $string, string $suffix, &$nonSuffix = null) : bool
44
    {
45
        $suffixLength = strlen($suffix);
46
47
        if ($suffixLength === 0) {
48
            $nonSuffix = $string;
49
            return true;
50
        }
51
52
        if (empty($string)) {
53
            $nonSuffix = '';
54
            return false;
55
        }
56
57
        if (substr_compare($string, $suffix, -$suffixLength, $suffixLength) !== 0) {
58
            $nonSuffix = $string;
59
            return false;
60
        }
61
62
        $nonSuffix = substr($string, 0, -$suffixLength);
63
        return true;
64
    }
65
66
    /**
67
     * Truncates the string to the given length, with an ellipsis at the end.
68
     *
69
     * @param string $string The string to shorten.
70
     * @param int $maxLength The length to truncate the string to.  The result will not be longer than this, but may be
71
     *                       shorter.
72
     * @param string $suffix The string to append when truncating.  Typically this will be an ellipsis.
73
     *
74
     * @return string The truncated string with the ellipsis included if truncation occured.
75
     *
76
     * @throws \InvalidArgumentException if $maxLength is negative
77
     */
78
    public static function ellipsize(string $string, int $maxLength, string $suffix = '...') : string
79
    {
80
        if ($maxLength < 0) {
81
            throw new \InvalidArgumentException('$maxLength is negative');
82
        }
83
84
        if (strlen($string) <= $maxLength) {
85
            return $string;
86
        }
87
88
        $trimmedLength = $maxLength - strlen($suffix);
89
        $string = substr($string, 0, max(0, $trimmedLength));
90
91
        if ($string === '') {
92
            return substr($suffix, 0, $maxLength);
93
        }
94
95
        return $string . $suffix;
96
    }
97
98
    /**
99
     * Uppercases words using custom word delimiters.
100
     *
101
     * This is more flexible than normal php ucwords because that only treats space as a word delimiter.
102
     *
103
     * Here is an example:
104
     * <code>
105
     * <?php
106
     * $string = 'break-down o\'boy up_town you+me here now,this:place';
107
     *
108
     * echo String::ucwords($string);
109
     * // Break-Down O\'Boy Up_Town You+Me Here Now,This:Place
110
     *
111
     * echo String::ucwords($string, '- ');
112
     * // Break-Down O\'boy Up_town You+me Here Now,this:place
113
     * ?>
114
     * </code>
115
     *
116
     * @param string $string The string to titleize.
117
     * @param string $delimiters The characters to treat as word delimiters.
118
     *
119
     * @return string The titleized string.
120
     */
121
    public static function ucwords(string $string, string $delimiters = "-_+' \n\t\r\0\x0B:/,.") : string
122
    {
123
        if ($delimiters === '') {
124
            return $string;
125
        }
126
127
        return preg_replace_callback(
128
            '/[^' . preg_quote($delimiters, '/') . ']+/',
129
            function ($matches) {
130
                return ucfirst($matches[0]);
131
            },
132
            $string
133
        );
134
    }
135
}
136