Completed
Push — master ( 888d29...e13826 )
by Sebastian
06:20
created

StringHelper::mb_ucfirst()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is a part of HDS (HeBIS Discovery System). HDS is an 
4
 * extension of the open source library search engine VuFind, that 
5
 * allows users to search and browse beyond resources. More 
6
 * Information about VuFind you will find on http://www.vufind.org
7
 * 
8
 * Copyright (C) 2016 
9
 * HeBIS Verbundzentrale des HeBIS-Verbundes 
10
 * Goethe-Universität Frankfurt / Goethe University of Frankfurt
11
 * http://www.hebis.de
12
 * 
13
 * This program is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU General Public License
15
 * as published by the Free Software Foundation; either version 2
16
 * of the License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26
 */
27
28
namespace Seboettg\CiteProc\Util;
29
30
31
/**
32
 * Class StringHelper
33
 * @package Seboettg\CiteProc\Util
34
 *
35
 * @author Sebastian Böttger <[email protected]>
36
 */
37
class StringHelper
38
{
39
40
    const PREPOSITIONS = [
41
        'on', 'in', 'at', 'since', 'for', 'ago', 'before', 'to', 'past', 'till', 'until', 'by', 'under', 'below', 'over',
42
        'above', 'across', 'through', 'into', 'towards', 'onto', 'from', 'of', 'off', 'about', 'via'
43
    ];
44
45
    const ARTICLES = [
46
        'a', 'an', 'the'
47
    ];
48
49
    const ADVERBS = [
50
        'yet', 'so', 'just', 'only'
51
    ];
52
53
    const CONJUNCTIONS = [
54
        'nor', 'so', 'and', 'or'
55
    ];
56
57
    const ADJECTIVES = [
58
        'down', 'up'
59
    ];
60
61
62
    public static function capitalizeAll($text)
63
    {
64
        $wordArray = explode(" ", $text);
65
66
        array_walk($wordArray, function (&$word) {
67
            $word = ucfirst($word);
68
        });
69
70
        return implode(" ", $wordArray);
71
    }
72
73
    public static function capitalizeForTitle($titleString)
74
    {
75
        if (preg_match('/(.+[^\<\>][\.:\/;\?\!]\s?)([a-z])(.+)/', $titleString, $match)) {
76
            $titleString = $match[1].StringHelper::mb_ucfirst($match[2]).$match[3];
77
        }
78
79
        $wordArray = explode(" ", $titleString);
80
81
        array_walk($wordArray, function (&$word) {
82
83
            $words = explode("-", $word);
84
            if (count($words) > 1) {
85
                array_walk($words, function (&$w) {
86
                    $w = StringHelper::keepLowerCase($w) ? $w : StringHelper::mb_ucfirst($w);
87
                });
88
                $word = implode("-", $words);
89
            }
90
            $word = StringHelper::keepLowerCase($word) ? $word : StringHelper::mb_ucfirst($word);
91
        });
92
93
        return implode(" ", $wordArray);
94
    }
95
96
    public static function keepLowerCase($word)
97
    {
98
        $lowerCase =  in_array($word, self::PREPOSITIONS) ||
99
                      in_array($word, self::ARTICLES) ||
100
                      in_array($word, self::CONJUNCTIONS) ||
101
                      in_array($word, self::ADJECTIVES);
102
        return $lowerCase;
103
104
    }
105
106
    public static function mb_ucfirst($string, $encoding = 'UTF-8')
107
    {
108
        $strlen = mb_strlen($string, $encoding);
109
        $firstChar = mb_substr($string, 0, 1, $encoding);
110
        $then = mb_substr($string, 1, $strlen - 1, $encoding);
111
        $encodings = ['ISO-8859-7'];
112
        $encoding = mb_detect_encoding($firstChar, $encodings, true);
113
        return in_array($encoding, $encodings) ? $firstChar.$then : mb_strtoupper($firstChar, $encoding) . $then;
114
    }
115
}