View source
<?php
class Braintree_Util {
public static function extractAttributeAsArray(&$attribArray, $attributeName) {
if (!isset($attribArray[$attributeName])) {
return array();
}
$data = $attribArray[$attributeName];
$classFactory = self::buildClassName($attributeName) . '::factory';
if (is_array($data)) {
$objectArray = array_map($classFactory, $data);
}
else {
return array(
$data,
);
}
unset($attribArray[$attributeName]);
return $objectArray;
}
public static function throwStatusCodeException($statusCode, $message = null) {
switch ($statusCode) {
case 401:
throw new Braintree_Exception_Authentication();
break;
case 403:
throw new Braintree_Exception_Authorization($message);
break;
case 404:
throw new Braintree_Exception_NotFound();
break;
case 426:
throw new Braintree_Exception_UpgradeRequired();
break;
case 500:
throw new Braintree_Exception_ServerError();
break;
case 503:
throw new Braintree_Exception_DownForMaintenance();
break;
default:
throw new Braintree_Exception_Unexpected('Unexpected HTTP_RESPONSE #' . $statusCode);
break;
}
}
public static function cleanClassName($name) {
$name = str_replace('Braintree_', '', $name);
if (false === function_exists('lcfirst')) {
function lcfirst($str) {
return (string) (strtolower(substr($str, 0, 1)) . substr($str, 1));
}
}
return lcfirst($name);
}
public static function buildClassName($name) {
return 'Braintree_' . ucfirst($name);
}
public static function delimiterToCamelCase($string, $delimiter = '[\\-\\_]') {
return preg_replace('/' . $delimiter . '(\\w)/e', 'strtoupper("$1")', $string);
}
public static function delimiterToUnderscore($string) {
return preg_replace('/-/', '_', $string);
}
public static function camelCaseToDelimiter($string, $delimiter = '-') {
return preg_replace('/([A-Z])/e', '"' . $delimiter . '" . strtolower("$1")', $string);
}
public static function implodeAssociativeArray($array, $separator = '=', $glue = ', ') {
$tmpArray = null;
foreach ($array as $key => $value) {
$tmpArray[] = $key . $separator . $value;
}
return is_array($tmpArray) ? implode($glue, $tmpArray) : false;
}
public static function attributesToString($attributes) {
foreach ($attributes as $key => $value) {
if (is_array($value)) {
$pAttrib = "";
foreach ($value as $obj) {
$pAttrib .= sprintf('%s', $obj);
}
}
else {
if ($value instanceof DateTime) {
$pAttrib = $value
->format(DateTime::RFC850);
}
else {
$pAttrib = $value;
}
}
$printableAttribs[$key] = sprintf('%s', $pAttrib);
}
return Braintree_Util::implodeAssociativeArray($printableAttribs);
}
public static function verifyKeys($signature, $attributes) {
$validKeys = self::_flattenArray($signature);
$userKeys = self::_flattenUserKeys($attributes);
$invalidKeys = array_diff($userKeys, $validKeys);
$invalidKeys = self::_removeWildcardKeys($validKeys, $invalidKeys);
if (!empty($invalidKeys)) {
asort($invalidKeys);
$sortedList = join(', ', $invalidKeys);
throw new InvalidArgumentException('invalid keys: ' . $sortedList);
}
}
private static function _flattenArray($keys, $namespace = null) {
$flattenedArray = array();
foreach ($keys as $key) {
if (is_array($key)) {
$theKeys = array_keys($key);
$theValues = array_values($key);
$scope = $theKeys[0];
$fullKey = empty($namespace) ? $scope : $namespace . '[' . $scope . ']';
$flattenedArray = array_merge($flattenedArray, self::_flattenArray($theValues[0], $fullKey));
}
else {
$fullKey = empty($namespace) ? $key : $namespace . '[' . $key . ']';
$flattenedArray[] = $fullKey;
}
}
sort($flattenedArray);
return $flattenedArray;
}
private static function _flattenUserKeys($keys, $namespace = null) {
$flattenedArray = array();
foreach ($keys as $key => $value) {
$fullKey = empty($namespace) ? $key : $namespace;
if (!is_numeric($key) && $namespace != null) {
$fullKey .= '[' . $key . ']';
}
if (is_numeric($key) && is_string($value)) {
$fullKey .= '[' . $value . ']';
}
if (is_array($value)) {
$more = self::_flattenUserKeys($value, $fullKey);
$flattenedArray = array_merge($flattenedArray, $more);
}
else {
$flattenedArray[] = $fullKey;
}
}
sort($flattenedArray);
return $flattenedArray;
}
private static function _removeWildcardKeys($validKeys, $invalidKeys) {
foreach ($validKeys as $key) {
if (stristr($key, '[_anyKey_]')) {
$wildcardKey = str_replace('[_anyKey_]', '', $key);
foreach ($invalidKeys as $index => $invalidKey) {
if (stristr($invalidKey, $wildcardKey)) {
unset($invalidKeys[$index]);
}
}
}
}
return $invalidKeys;
}
}