StringHelper::jsonDecode()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 21
rs 9.6111
cc 5
nc 6
nop 1
1
<?php
2
3
/**
4
 * PHP version 5.4 and 8
5
 *
6
 * @category  Helper
7
 * @package   Payever\Core
8
 * @author    payever GmbH <[email protected]>
9
 * @copyright 2017-2021 payever GmbH
10
 * @license   MIT <https://opensource.org/licenses/MIT>
11
 * @link      https://docs.payever.org/shopsystems/api/getting-started
12
 */
13
14
namespace Payever\ExternalIntegration\Core\Helper;
15
16
/**
17
 * This class represents helper functions for strings
18
 * @SuppressWarnings(PHPMD.MissingImport)
19
 */
20
class StringHelper
21
{
22
    /**
23
     * Converts field names for setters and getters
24
     *
25
     * @param string $name
26
     *
27
     * @return string
28
     */
29
    public static function underscore($name)
30
    {
31
        return strtolower(preg_replace('/(.)([A-Z0-9])/', "$1_$2", $name));
32
    }
33
34
    /**
35
     * Will capitalize word's first letters and convert separators if needed
36
     *
37
     * @param string $name
38
     * @param bool   $firstLetter
39
     *
40
     * @return string
41
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
42
     */
43
    public static function camelize($name, $firstLetter = false)
44
    {
45
        $string = str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
46
47
        if (!$firstLetter) {
48
            $string = lcfirst($string);
49
        }
50
51
        return $string;
52
    }
53
54
    /**
55
     * Returns decoded JSON
56
     *
57
     * @param array|object|\stdClass|string $object
58
     *
59
     * @return bool|mixed
60
     * @throws \UnexpectedValueException
61
     */
62
    public static function jsonDecode($object)
63
    {
64
        if (!is_string($object)) {
65
            return $object;
66
        }
67
68
        $result = json_decode($object);
69
        if (function_exists('json_last_error')) {
70
            if (json_last_error() != JSON_ERROR_NONE) {
71
                throw new \UnexpectedValueException(
72
                    'JSON Decode Error: ' . json_last_error(),
73
                    json_last_error()
74
                );
75
            }
76
        }
77
78
        if (!empty($result)) {
79
            return $result;
80
        }
81
82
        return false;
83
    }
84
}
85