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