Str   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 45
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A convertDateTime() 0 11 2
A convertToCamelCase() 0 6 1
A studly() 0 6 1
1
<?php
2
/*
3
 *   This file is part of the Vultr PHP library.
4
 *
5
 *   (c) Albert Leitato <[email protected]>
6
 *
7
 *   For the full copyright and license information, please view the LICENSE
8
 *   file that was distributed with this source code.
9
 */
10
namespace Vultr\Support;
11
12
/**
13
 * @author Albert Leitato <[email protected]>
14
 */
15
trait Str
16
{
17
    /**
18
     * @param string|null $date DateTime string
19
     *
20
     * @return string|null DateTime in ISO8601 format
21
     */
22
    public static function convertDateTime($date)
23
    {
24
        if (!$date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25
            return;
26
        }
27
28
        $date = new \DateTime($date);
29
        $date->setTimezone(new \DateTimeZone(\date_default_timezone_get()));
30
31
        return $date->format(\DateTime::ISO8601);
32
    }
33
34
    /**
35
     * @param string $str Snake case string
36
     *
37
     * @return string Camel case string
38
     */
39
    public static function convertToCamelCase($str)
40
    {
41
        $string =  static::studly($str);
42
43
        return \lcfirst($string);
44
    }
45
46
    /**
47
     * Convert a value to studly caps case.
48
     *
49
     * @param string $value
50
     *
51
     * @return string
52
     */
53
    public static function studly($value)
54
    {
55
        $value = \ucwords(\str_replace(['-', '_'], ' ', \strtolower($value)));
56
57
        return \str_replace(' ', '', $value);
58
    }
59
}
60