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

Str   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
c 2
b 1
f 0
lcom 0
cbo 0
dl 0
loc 66
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A studly() 0 14 2
A append() 0 8 2
A appendWith() 0 8 2
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