Passed
Push — master ( e13826...b82956 )
by Sebastian
16:47 queued 09:52
created

StringHelper::camelCase2Hyphen()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
13
/**
14
 * Class StringHelper
15
 * @package Seboettg\CiteProc\Util
16
 *
17
 * @author Sebastian Böttger <[email protected]>
18
 */
19
class StringHelper
20
{
21
22
    const PREPOSITIONS = [
23
        'on', 'in', 'at', 'since', 'for', 'ago', 'before', 'to', 'past', 'till', 'until', 'by', 'under', 'below', 'over',
24
        'above', 'across', 'through', 'into', 'towards', 'onto', 'from', 'of', 'off', 'about', 'via'
25
    ];
26
27
    const ARTICLES = [
28
        'a', 'an', 'the'
29
    ];
30
31
    const ADVERBS = [
32
        'yet', 'so', 'just', 'only'
33
    ];
34
35
    const CONJUNCTIONS = [
36
        'nor', 'so', 'and', 'or'
37
    ];
38
39
    const ADJECTIVES = [
40
        'down', 'up'
41
    ];
42
43
44
    public static function capitalizeAll($text)
45
    {
46
        $wordArray = explode(" ", $text);
47
48
        array_walk($wordArray, function (&$word) {
49
            $word = ucfirst($word);
50
        });
51
52
        return implode(" ", $wordArray);
53
    }
54
55
    public static function capitalizeForTitle($titleString)
56
    {
57
        if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) {
58
            $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...
59
        }
60
61
        $wordArray = explode(" ", $titleString);
62
63
        array_walk($wordArray, function (&$word) {
64
65
            $words = explode("-", $word);
66
            if (count($words) > 1) {
67
                array_walk($words, function (&$w) {
68
                    $w = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
69
                });
70
                $word = implode("-", $words);
71
            }
72
            $word = StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
73
        });
74
75
        return implode(" ", $wordArray);
76
    }
77
78
    public static function keepLowerCase($word)
79
    {
80
        $lowerCase =  in_array($word, self::PREPOSITIONS) ||
81
                      in_array($word, self::ARTICLES) ||
82
                      in_array($word, self::CONJUNCTIONS) ||
83
                      in_array($word, self::ADJECTIVES);
84
        return $lowerCase;
85
86
    }
87
88
    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...
89
    {
90
        $strlen = mb_strlen($string, $encoding);
91
        $firstChar = mb_substr($string, 0, 1, $encoding);
92
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
93
        $encodings = ['ISO-8859-7'];
94
        $encoding = mb_detect_encoding($firstChar, $encodings, true);
95
        return in_array($encoding, $encodings) ? $firstChar.$then : mb_strtoupper($firstChar, $encoding) . $then;
96
    }
97
98
    public static function explodeBySpaceOrHyphen($string)
99
    {
100
        $res = [];
101
        $exploded = explode("-", $string);
102
        foreach ($exploded as $explode) {
103
            $spaceExploded = explode(" ", $explode);
104
            $res = array_merge($res, $spaceExploded);
105
        }
106
        return $res;
107
    }
108
109
    public static function camelCase2Hyphen($string)
110
    {
111
        $hyphenated = preg_replace("/([A-Z])/", "-$1", $string);
112
        $hyphenated = substr($hyphenated, 0, 1) === "-" ? substr($hyphenated, 1) : $hyphenated;
113
        return mb_strtolower($hyphenated);
114
    }
115
}