Completed
Push — 2.x ( 0c3f5d...21439c )
by Paul
10s
created

AbstractCharCase::ucfirst()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Rule;
10
11
use Aura\Filter\Exception;
12
13
/**
14
 *
15
 * Abstract rule for character case filters; supports the `mbstring`
16
 * extension.
17
 *
18
 * @package Aura.Filter
19
 *
20
 */
21
abstract class AbstractCharCase extends AbstractStrlen
22
{
23
    /**
24
     *
25
     * Proxy to `mb_convert_case()` when available; fall back to
26
     * `utf8_decode()` and `strtolower()` otherwise.
27
     *
28
     * @param string $str String to convert case.
29
     *
30
     * @return string
31
     *
32
     */
33 30
    protected function strtolower($str)
34
    {
35 30
        if ($this->mbstring()) {
36 29
            return mb_convert_case($str, MB_CASE_LOWER, 'UTF-8');
37
        }
38
39 1
        return strtolower(utf8_decode($str));
40
    }
41
42
    /**
43
     *
44
     * Proxy to `mb_convert_case()` when available; fall back to
45
     * `utf8_decode()` and `strtoupper()` otherwise.
46
     *
47
     * @param string $str String to convert case.
48
     *
49
     * @return string
50
     *
51
     */
52 30
    protected function strtoupper($str)
53
    {
54 30
        if ($this->mbstring()) {
55 29
            return mb_convert_case($str, MB_CASE_UPPER, 'UTF-8');
56
        }
57
58 1
        return strtoupper(utf8_decode($str));
59
    }
60
61
    /**
62
     *
63
     * Proxy to `mb_convert_case()` when available; fall back to
64
     * `utf8_decode()` and `ucwords()` otherwise.
65
     *
66
     * @param string $str String to convert case.
67
     *
68
     * @return int
69
     *
70
     */
71 15
    protected function ucwords($str)
72
    {
73 15
        if ($this->mbstring()) {
74 14
            return mb_convert_case($str, MB_CASE_TITLE, 'UTF-8');
75
        }
76
77 1
        return ucwords(utf8_decode($str));
78
    }
79
80
    /**
81
     *
82
     * Proxy to `mb_convert_case()` when available; fall back to
83
     * `utf8_decode()` and `strtoupper()` otherwise.
84
     *
85
     * @param string $str String to convert case.
86
     *
87
     * @return int
88
     *
89
     */
90 15
    protected function ucfirst($str)
91
    {
92 15
        $len = $this->strlen($str);
93 15
        if ($len == 0) {
94 1
            return '';
95
        }
96 14
        if ($len > 1) {
97 12
            $head = $this->substr($str, 0, 1);
98 12
            $tail = $this->substr($str, 1, $len - 1);
99 12
            return $this->strtoupper($head) . $tail;
100
        }
101 2
        return $this->strtoupper($str);
102
    }
103
104
    /**
105
     *
106
     * Proxy to `mb_convert_case()` when available; fall back to
107
     * `utf8_decode()` and `strtolower()` otherwise.
108
     *
109
     * @param string $str String to convert case.
110
     *
111
     * @return int
112
     *
113
     */
114 15
    protected function lcfirst($str)
115
    {
116 15
        $len = $this->strlen($str);
117 15
        if ($len == 0) {
118
            // empty string
119 1
            return '';
120
        }
121 14
        if ($len > 1) {
122
            // more than a single character
123 12
            $head = $this->substr($str, 0, 1);
124 12
            $tail = $this->substr($str, 1, $len - 1);
125 12
            return $this->strtolower($head) . $tail;
126
        }
127 2
        return $this->strtolower($str);
128
    }
129
130
}
131