Completed
Pull Request — 2.x (#106)
by jake
02:02
created

AbstractCharCase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 8
c 2
b 0
f 2
lcom 1
cbo 1
dl 0
loc 98
ccs 22
cts 22
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A strtolower() 0 8 2
A strtoupper() 0 8 2
A ucwords() 0 8 2
A ucfirst() 0 8 1
A lcfirst() 0 8 1
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 23
    protected function strtolower($str)
34
    {
35 23
        if ($this->mbstring()) {
36 22
            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 23
    protected function strtoupper($str)
53
    {
54 23
        if ($this->mbstring()) {
55 22
            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 8
    protected function ucfirst($str)
91
    {
92 8
        $len = $this->strlen($str);
93 8
        $head = $this->substr($str, 0, 1);
94 8
        $tail = $this->substr($str, 1, $len - 1);
95
96 8
        return $this->strtoupper($head) . $tail;
97
    }
98
99
    /**
100
     *
101
     * Proxy to `mb_convert_case()` when available; fall back to
102
     * `utf8_decode()` and `strtolower()` otherwise.
103
     *
104
     * @param string $str String to convert case.
105
     *
106
     * @return int
107
     *
108
     */
109 8
    protected function lcfirst($str)
110
    {
111 8
        $len = $this->strlen($str);
112 8
        $head = $this->substr($str, 0, 1);
113 8
        $tail = $this->substr($str, 1, $len - 1);
114
115 8
        return $this->strtolower($head) . $tail;
116
    }
117
118
}
119