Completed
Push — master ( d6a1be...0d1f44 )
by Barry vd.
12s queued 10s
created

Helper::validateLuhn()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Helper class
4
 */
5
6
namespace Omnipay\Common;
7
8
use InvalidArgumentException;
9
10
/**
11
 * Helper class
12
 *
13
 * This class defines various static utility functions that are in use
14
 * throughout the Omnipay system.
15
 */
16
class Helper
17
{
18
    /**
19
     * Convert a string to camelCase. Strings already in camelCase will not be harmed.
20
     *
21
     * @param  string  $str The input string
22
     * @return string camelCased output string
23
     */
24 75
    public static function camelCase($str)
25
    {
26 75
        $str = self::convertToLowercase($str);
27 75
        return preg_replace_callback(
28 75
            '/_([a-z])/',
29 75
            function ($match) {
30 6
                return strtoupper($match[1]);
31 75
            },
32 75
            $str
33
        );
34
    }
35
36
    /**
37
     * Convert strings with underscores to be all lowercase before camelCase is preformed.
38
     *
39
     * @param  string $str The input string
40
     * @return string The output string
41
     */
42 75
    protected static function convertToLowercase($str)
43
    {
44 75
        $explodedStr = explode('_', $str);
45 75
        $lowercasedStr = [];
46
47 75
        if (count($explodedStr) > 1) {
48 6
            foreach ($explodedStr as $value) {
49 6
                $lowercasedStr[] = strtolower($value);
50
            }
51 6
            $str = implode('_', $lowercasedStr);
52
        }
53
54 75
        return $str;
55
    }
56
57
    /**
58
     * Validate a card number according to the Luhn algorithm.
59
     *
60
     * @param  string  $number The card number to validate
61
     * @return boolean True if the supplied card number is valid
62
     */
63 21
    public static function validateLuhn($number)
64
    {
65 21
        $str = '';
66 21
        foreach (array_reverse(str_split($number)) as $i => $c) {
67 21
            $str .= $i % 2 ? $c * 2 : $c;
68
        }
69
70 21
        return array_sum(str_split($str)) % 10 === 0;
71
    }
72
73
    /**
74
     * Initialize an object with a given array of parameters
75
     *
76
     * Parameters are automatically converted to camelCase. Any parameters which do
77
     * not match a setter on the target object are ignored.
78
     *
79
     * @param mixed $target     The object to set parameters on
80
     * @param array $parameters An array of parameters to set
81
     */
82 567
    public static function initialize($target, array $parameters = null)
83
    {
84 567
        if ($parameters) {
85 66
            foreach ($parameters as $key => $value) {
86 66
                $method = 'set'.ucfirst(static::camelCase($key));
87 66
                if (method_exists($target, $method)) {
88 66
                    $target->$method($value);
89
                }
90
            }
91
        }
92 567
    }
93
94
    /**
95
     * Resolve a gateway class to a short name.
96
     *
97
     * The short name can be used with GatewayFactory as an alias of the gateway class,
98
     * to create new instances of a gateway.
99
     */
100 18
    public static function getGatewayShortName($className)
101
    {
102 18
        if (0 === strpos($className, '\\')) {
103 9
            $className = substr($className, 1);
104
        }
105
106 18
        if (0 === strpos($className, 'Omnipay\\')) {
107 15
            return trim(str_replace('\\', '_', substr($className, 8, -7)), '_');
108
        }
109
110 3
        return '\\'.$className;
111
    }
112
113
    /**
114
     * Resolve a short gateway name to a full namespaced gateway class.
115
     *
116
     * Class names beginning with a namespace marker (\) are left intact.
117
     * Non-namespaced classes are expected to be in the \Omnipay namespace, e.g.:
118
     *
119
     *      \Custom\Gateway     => \Custom\Gateway
120
     *      \Custom_Gateway     => \Custom_Gateway
121
     *      Stripe              => \Omnipay\Stripe\Gateway
122
     *      PayPal\Express      => \Omnipay\PayPal\ExpressGateway
123
     *      PayPal_Express      => \Omnipay\PayPal\ExpressGateway
124
     *
125
     * @param  string  $shortName The short gateway name
126
     * @return string  The fully namespaced gateway class name
127
     */
128 24
    public static function getGatewayClassName($shortName)
129
    {
130 24
        if (0 === strpos($shortName, '\\')) {
131 9
            return $shortName;
132
        }
133
134
        // replace underscores with namespace marker, PSR-0 style
135 15
        $shortName = str_replace('_', '\\', $shortName);
136 15
        if (false === strpos($shortName, '\\')) {
137 6
            $shortName .= '\\';
138
        }
139
140 15
        return '\\Omnipay\\'.$shortName.'Gateway';
141
    }
142
}
143