Completed
Push — master ( e41910...8b07f6 )
by Zach
02:33
created

Str::appendWith()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Yarak\Helpers;
4
5
class Str
6
{
7
    /**
8
     * The cache of studly-cased words.
9
     *
10
     * @var array
11
     */
12
    protected static $studlyCache = [];
13
14
    /**
15
     * Create StudlyCase string from _ separated string.
16
     *
17
     * @param string $value
18
     *
19
     * @return string
20
     */
21
    public static function studly($value)
22
    {
23
        static $studlyCache = [];
24
25
        $key = $value;
26
27
        if (isset($studlyCache[$key])) {
28
            return $studlyCache[$key];
29
        }
30
31
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
32
33
        return $studlyCache[$key] = str_replace(' ', '', $value);
34
    }
35
36
    /**
37
     * Append a character to a string if it doesn't already exist.
38
     *
39
     * @param string $value
40
     * @param string $char
41
     *
42
     * @return string
43
     */
44
    public static function append($value, $char)
45
    {
46
        if (substr($value, -1) !== $char) {
47
            $value .= $char;
48
        }
49
50
        return $value;
51
    }
52
53
    /**
54
     * Append a string to value and place single instance of $with between.
55
     *
56
     * @param string $value
57
     * @param string $append
58
     * @param string $with
59
     *
60
     * @return string
61
     */
62
    public static function appendWith($value, $append, $with)
63
    {
64
        if (!empty($append)) {
65
            return rtrim($value, $with).$with.ltrim($append, $with);
66
        }
67
68
        return $value;
69
    }
70
}
71