Passed
Push — master ( c41dc5...63b79a )
by Sebastian
03:21
created

StringHelper   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A capitalizeAll() 0 10 1
B capitalizeForTitle() 0 22 5
A keepLowerCase() 0 9 4
A mb_ucfirst() 0 9 2
A initializeBySpaceOrHyphen() 0 17 4
A camelCase2Hyphen() 0 6 2
A checkLowerCaseString() 0 4 1
A checkUpperCaseString() 0 4 1
1
<?php
2
/**
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Util;
11
12
use Symfony\Polyfill\Mbstring\Mbstring;
13
14
/**
15
 * Class StringHelper
16
 * @package Seboettg\CiteProc\Util
17
 *
18
 * @author Sebastian Böttger <[email protected]>
19
 */
20
class StringHelper
21
{
22
23
    const PREPOSITIONS = [
24
        'on', 'in', 'at', 'since', 'for', 'ago', 'before', 'to', 'past', 'till', 'until', 'by', 'under', 'below', 'over',
25
        'above', 'across', 'through', 'into', 'towards', 'onto', 'from', 'of', 'off', 'about', 'via'
26
    ];
27
28
    const ARTICLES = [
29
        'a', 'an', 'the'
30
    ];
31
32
    const ADVERBS = [
33
        'yet', 'so', 'just', 'only'
34
    ];
35
36
    const CONJUNCTIONS = [
37
        'nor', 'so', 'and', 'or'
38
    ];
39
40
    const ADJECTIVES = [
41
        'down', 'up'
42
    ];
43
44
    const ISO_ENCODINGS = [
45
        'ISO-8859-1',
46
        'ISO-8859-2',
47
        'ISO-8859-3',
48
        'ISO-8859-4',
49
        'ISO-8859-5',
50
        'ISO-8859-6',
51
        'ISO-8859-7',
52
        'ISO-8859-8',
53
        'ISO-8859-9',
54
        'ISO-8859-10',
55
        'ISO-8859-11',
56
        'ISO-8859-13',
57
        'ISO-8859-14',
58
        'ISO-8859-15',
59
        'ISO-8859-16'
60
    ];
61
62
63
    public static function capitalizeAll($text)
64
    {
65
        $wordArray = explode(" ", $text);
66
67
        array_walk($wordArray, function (&$word) {
68
            $word = ucfirst($word);
69
        });
70
71
        return implode(" ", $wordArray);
72
    }
73
74
    public static function capitalizeForTitle($titleString)
75
    {
76
        if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) {
77
            $titleString = $match[1].StringHelper::mb_ucfirst($match[2]).$match[3];
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
78
        }
79
80
        $wordArray = explode(" ", $titleString);
81
82
        array_walk($wordArray, function (&$word) {
83
84
            $words = explode("-", $word);
85
            if (count($words) > 1) {
86
                array_walk($words, function (&$w) {
87
                    $w = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
88
                });
89
                $word = implode("-", $words);
90
            }
91
            $word = StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
92
        });
93
94
        return implode(" ", $wordArray);
95
    }
96
97
    public static function keepLowerCase($word)
98
    {
99
        $lowerCase =  in_array($word, self::PREPOSITIONS) ||
100
            in_array($word, self::ARTICLES) ||
101
            in_array($word, self::CONJUNCTIONS) ||
102
            in_array($word, self::ADJECTIVES);
103
        return $lowerCase;
104
105
    }
106
107
    public static function mb_ucfirst($string, $encoding = 'UTF-8')
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
108
    {
109
        $strlen = mb_strlen($string, $encoding);
110
        $firstChar = mb_substr($string, 0, 1, $encoding);
111
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
112
113
        $encoding = Mbstring::mb_detect_encoding($firstChar, self::ISO_ENCODINGS, true);
114
        return in_array($encoding, self::ISO_ENCODINGS) ? Mbstring::mb_strtoupper($firstChar, $encoding) . $then : $firstChar . $then;
115
    }
116
117
    public static function initializeBySpaceOrHyphen($string, $initializeSign)
118
    {
119
        $res = "";
120
        $exploded = explode("-", $string);
121
        $i = 0;
122
        foreach ($exploded as $explode) {
123
            $spaceExploded = explode(" ", $explode);
124
            foreach ($spaceExploded as $givenPart) {
125
                $res .= substr($givenPart, 0, 1) . $initializeSign;
126
            }
127
            if ($i < count($exploded) - 1) {
128
                $res = rtrim($res) . "-";
129
            }
130
            ++$i;
131
        }
132
        return $res;
133
    }
134
135
    public static function camelCase2Hyphen($string)
136
    {
137
        $hyphenated = preg_replace("/([A-Z])/", "-$1", $string);
138
        $hyphenated = substr($hyphenated, 0, 1) === "-" ? substr($hyphenated, 1) : $hyphenated;
139
        return mb_strtolower($hyphenated);
140
    }
141
142
    public static function checkLowerCaseString($string)
143
    {
144
        return ($string === mb_strtolower($string));
145
    }
146
147
    public static function checkUpperCaseString($string)
148
    {
149
        return ($string === mb_strtoupper($string));
150
    }
151
152
}