Helper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
c 2
b 0
f 0
dl 0
loc 59
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceFirst() 0 13 2
A startsWith() 0 3 1
A toCamelCase() 0 3 1
A getMajorVersion() 0 9 2
1
<?php
2
3
/**
4
 * Platine User Agent
5
 *
6
 * Platine User Agent is a lightweight library for detecting
7
 * user browser, device, OS, CPU
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine User Agent
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file Helper.php
34
 *
35
 *  The User Agent helper class
36
 *
37
 *  @package    Platine\UserAgent\Util
38
 *  @author Platine Developers Team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\UserAgent\Util;
49
50
/**
51
 * @class Helper
52
 * @package Platine\UserAgent\Util
53
 */
54
class Helper
55
{
56
    /**
57
     * Return the major version number from the full version
58
     * @param string $fullVersion
59
     * @return int
60
     */
61
    public static function getMajorVersion(string $fullVersion): int
62
    {
63
        $version = preg_replace('/[^\d\.]/', '', $fullVersion);
64
        if ($version !== null) {
65
            $parts = explode('.', $version);
66
67
            return (int) $parts[0];
68
        }
69
        return 0;
70
    }
71
72
    /**
73
     * Return the camel case value of the given string
74
     * @param string $value
75
     * @return string
76
     */
77
    public static function toCamelCase(string $value): string
78
    {
79
        return str_replace('_', '', lcfirst(ucwords($value, '_')));
80
    }
81
82
    /**
83
     * Check whether the string start with
84
     * @param string $str the string value
85
     * @param string $search the value to search
86
     * @return bool
87
     */
88
    public static function startsWith(string $str, string $search): bool
89
    {
90
        return strpos($str, $search) === 0;
91
    }
92
93
    /**
94
     * Replace the first matched of the string
95
     * @param string $search
96
     * @param string $replace
97
     * @param string $str
98
     * @return string
99
     */
100
    public static function replaceFirst(
101
        string $search,
102
        string $replace,
103
        string $str
104
    ): string {
105
        $regex = '/' . preg_quote($search, '/') . '/';
106
        $content = preg_replace($regex, $replace, $str, 1);
107
108
        if ($content !== null) {
109
            return $content;
110
        }
111
112
        return '';
113
    }
114
}
115