BraintreeUtil

Braintree Utility methods PHP version 5.

Defined (1)

The class is defined in the following location(s).

/includes/lib/Braintree/lib/Braintree/Util.php  
  1. class Util 
  2. /** 
  3. * extracts an attribute and returns an array of objects 
  4. * extracts the requested element from an array, and converts the contents 
  5. * of its child arrays to objects of type $attributeName, or returns 
  6. * an array with a single element containing the value of that array element 
  7. * @param array $attribArray attributes from a search response 
  8. * @param string $attributeName indicates which element of the passed array to extract 
  9. * @return array array of $attributeName objects, or a single element array 
  10. */ 
  11. public static function extractAttributeAsArray(&$attribArray, $attributeName) 
  12. if(!isset($attribArray[$attributeName])): 
  13. return []; 
  14. endif; 
  15.  
  16. // get what should be an array from the passed array 
  17. $data = $attribArray[$attributeName]; 
  18. // set up the class that will be used to convert each array element 
  19. $classFactory = self::buildClassName($attributeName) . '::factory'; 
  20. if(is_array($data)): 
  21. // create an object from the data in each element 
  22. $objectArray = array_map($classFactory, $data); 
  23. else: 
  24. return [$data]; 
  25. endif; 
  26.  
  27. unset($attribArray[$attributeName]); 
  28. return $objectArray; 
  29. /** 
  30. * throws an exception based on the type of error 
  31. * @param string $statusCode HTTP status code to throw exception from 
  32. * @param null|string $message 
  33. * @throws Exception multiple types depending on the error 
  34. * @return void 
  35. */ 
  36. public static function throwStatusCodeException($statusCode, $message=null) 
  37. switch($statusCode) { 
  38. case 401: 
  39. throw new Exception\Authentication(); 
  40. break; 
  41. case 403: 
  42. throw new Exception\Authorization($message); 
  43. break; 
  44. case 404: 
  45. throw new Exception\NotFound(); 
  46. break; 
  47. case 426: 
  48. throw new Exception\UpgradeRequired(); 
  49. break; 
  50. case 429: 
  51. throw new Exception\TooManyRequests(); 
  52. break; 
  53. case 500: 
  54. throw new Exception\ServerError(); 
  55. break; 
  56. case 503: 
  57. throw new Exception\DownForMaintenance(); 
  58. break; 
  59. default: 
  60. throw new Exception\Unexpected('Unexpected HTTP_RESPONSE #' . $statusCode); 
  61. break; 
  62.  
  63. /** 
  64. * @param string $className 
  65. * @param object $resultObj 
  66. * @return object returns the passed object if successful 
  67. * @throws Exception\ValidationsFailed 
  68. */ 
  69. public static function returnObjectOrThrowException($className, $resultObj) 
  70. $resultObjName = self::cleanClassName($className); 
  71. if ($resultObj->success) { 
  72. return $resultObj->$resultObjName; 
  73. } else { 
  74. throw new Exception\ValidationsFailed(); 
  75.  
  76. /** 
  77. * removes the header from a classname 
  78. * @param string $name ClassName 
  79. * @return camelCased classname minus header 
  80. */ 
  81. public static function cleanClassName($name) 
  82. $classNamesToResponseKeys = [ 
  83. 'Braintree\CreditCard' => 'creditCard',  
  84. 'Braintree_CreditCard' => 'creditCard',  
  85. 'Braintree\CreditCardGateway' => 'creditCard',  
  86. 'Braintree_CreditCardGateway' => 'creditCard',  
  87. 'Braintree\Customer' => 'customer',  
  88. 'Braintree_Customer' => 'customer',  
  89. 'Braintree\CustomerGateway' => 'customer',  
  90. 'Braintree_CustomerGateway' => 'customer',  
  91. 'Braintree\Subscription' => 'subscription',  
  92. 'Braintree_Subscription' => 'subscription',  
  93. 'Braintree\SubscriptionGateway' => 'subscription',  
  94. 'Braintree_SubscriptionGateway' => 'subscription',  
  95. 'Braintree\Transaction' => 'transaction',  
  96. 'Braintree_Transaction' => 'transaction',  
  97. 'Braintree\TransactionGateway' => 'transaction',  
  98. 'Braintree_TransactionGateway' => 'transaction',  
  99. 'Braintree\CreditCardVerification' => 'verification',  
  100. 'Braintree_CreditCardVerification' => 'verification',  
  101. 'Braintree\CreditCardVerificationGateway' => 'verification',  
  102. 'Braintree_CreditCardVerificationGateway' => 'verification',  
  103. 'Braintree\AddOn' => 'addOn',  
  104. 'Braintree_AddOn' => 'addOn',  
  105. 'Braintree\AddOnGateway' => 'addOn',  
  106. 'Braintree_AddOnGateway' => 'addOn',  
  107. 'Braintree\Discount' => 'discount',  
  108. 'Braintree_Discount' => 'discount',  
  109. 'Braintree\DiscountGateway' => 'discount',  
  110. 'Braintree_DiscountGateway' => 'discount',  
  111. 'Braintree\Plan' => 'plan',  
  112. 'Braintree_Plan' => 'plan',  
  113. 'Braintree\PlanGateway' => 'plan',  
  114. 'Braintree_PlanGateway' => 'plan',  
  115. 'Braintree\Address' => 'address',  
  116. 'Braintree_Address' => 'address',  
  117. 'Braintree\AddressGateway' => 'address',  
  118. 'Braintree_AddressGateway' => 'address',  
  119. 'Braintree\SettlementBatchSummary' => 'settlementBatchSummary',  
  120. 'Braintree_SettlementBatchSummary' => 'settlementBatchSummary',  
  121. 'Braintree\SettlementBatchSummaryGateway' => 'settlementBatchSummary',  
  122. 'Braintree_SettlementBatchSummaryGateway' => 'settlementBatchSummary',  
  123. 'Braintree\Merchant' => 'merchant',  
  124. 'Braintree_Merchant' => 'merchant',  
  125. 'Braintree\MerchantGateway' => 'merchant',  
  126. 'Braintree_MerchantGateway' => 'merchant',  
  127. 'Braintree\MerchantAccount' => 'merchantAccount',  
  128. 'Braintree_MerchantAccount' => 'merchantAccount',  
  129. 'Braintree\MerchantAccountGateway' => 'merchantAccount',  
  130. 'Braintree_MerchantAccountGateway' => 'merchantAccount',  
  131. 'Braintree\OAuthCredentials' => 'credentials',  
  132. 'Braintree_OAuthCredentials' => 'credentials',  
  133. 'Braintree\OAuthResult' => 'result',  
  134. 'Braintree_OAuthResult' => 'result',  
  135. 'Braintree\PayPalAccount' => 'paypalAccount',  
  136. 'Braintree_PayPalAccount' => 'paypalAccount',  
  137. 'Braintree\PayPalAccountGateway' => 'paypalAccount',  
  138. 'Braintree_PayPalAccountGateway' => 'paypalAccount',  
  139. ]; 
  140.  
  141. return $classNamesToResponseKeys[$name]; 
  142.  
  143. /** 
  144. * @param string $name className 
  145. * @return string ClassName 
  146. */ 
  147. public static function buildClassName($name) 
  148. $responseKeysToClassNames = [ 
  149. 'creditCard' => 'Braintree\CreditCard',  
  150. 'customer' => 'Braintree\Customer',  
  151. 'subscription' => 'Braintree\Subscription',  
  152. 'transaction' => 'Braintree\Transaction',  
  153. 'verification' => 'Braintree\CreditCardVerification',  
  154. 'addOn' => 'Braintree\AddOn',  
  155. 'discount' => 'Braintree\Discount',  
  156. 'plan' => 'Braintree\Plan',  
  157. 'address' => 'Braintree\Address',  
  158. 'settlementBatchSummary' => 'Braintree\SettlementBatchSummary',  
  159. 'merchantAccount' => 'Braintree\MerchantAccount',  
  160. ]; 
  161.  
  162. return (string) $responseKeysToClassNames[$name]; 
  163.  
  164. /** 
  165. * convert alpha-beta-gamma to alphaBetaGamma 
  166. * @access public 
  167. * @param string $string 
  168. * @param null|string $delimiter 
  169. * @return string modified string 
  170. */ 
  171. public static function delimiterToCamelCase($string, $delimiter = '[\-\_]') 
  172. // php doesn't garbage collect functions created by create_function() 
  173. // so use a static variable to avoid adding a new function to memory 
  174. // every time this function is called. 
  175. static $callback = null; 
  176. if ($callback === null) { 
  177. $callback = create_function('$matches', 'return strtoupper($matches[1]);'); 
  178.  
  179. return preg_replace_callback('/' . $delimiter . '(\w)/', $callback, $string); 
  180.  
  181. /** 
  182. * convert alpha-beta-gamma to alpha_beta_gamma 
  183. * @access public 
  184. * @param string $string 
  185. * @return string modified string 
  186. */ 
  187. public static function delimiterToUnderscore($string) 
  188. return preg_replace('/-/', '_', $string); 
  189.  
  190.  
  191. /** 
  192. * find capitals and convert to delimiter + lowercase 
  193. * @access public 
  194. * @param string $string 
  195. * @param null|string $delimiter 
  196. * @return string modified string 
  197. */ 
  198. public static function camelCaseToDelimiter($string, $delimiter = '-') 
  199. return strtolower(preg_replace('/([A-Z])/', "$delimiter\\1", $string)); 
  200.  
  201. public static function delimiterToCamelCaseArray($array, $delimiter = '[\-\_]') 
  202. $converted = []; 
  203. foreach ($array as $key => $value) { 
  204. if (is_string($key)) { 
  205. $key = self::delimiterToCamelCase($key, $delimiter); 
  206.  
  207. if (is_array($value)) { 
  208. // Make an exception for custom fields, which must be underscore (can't be 
  209. // camelCase). 
  210. if ($key === 'customFields') { 
  211. $value = self::delimiterToUnderscoreArray($value); 
  212. } else { 
  213. $value = self::delimiterToCamelCaseArray($value, $delimiter); 
  214. $converted[$key] = $value; 
  215. return $converted; 
  216.  
  217. public static function camelCaseToDelimiterArray($array, $delimiter = '-') 
  218. $converted = []; 
  219. foreach ($array as $key => $value) { 
  220. if (is_string($key)) { 
  221. $key = self::camelCaseToDelimiter($key, $delimiter); 
  222. if (is_array($value)) { 
  223. $value = self::camelCaseToDelimiterArray($value, $delimiter); 
  224. $converted[$key] = $value; 
  225. return $converted; 
  226.  
  227. public static function delimiterToUnderscoreArray($array) 
  228. $converted = []; 
  229. foreach ($array as $key => $value) { 
  230. $key = self::delimiterToUnderscore($key); 
  231. $converted[$key] = $value; 
  232. return $converted; 
  233.  
  234. /** 
  235. * @param array $array associative array to implode 
  236. * @param string $separator (optional, defaults to =) 
  237. * @param string $glue (optional, defaults to ', ') 
  238. * @return bool 
  239. */ 
  240. public static function implodeAssociativeArray($array, $separator = '=', $glue = ', ') 
  241. // build a new array with joined keys and values 
  242. $tmpArray = null; 
  243. foreach ($array AS $key => $value) { 
  244. if ($value instanceof DateTime) { 
  245. $value = $value->format('r'); 
  246. $tmpArray[] = $key . $separator . $value; 
  247. // implode and return the new array 
  248. return (is_array($tmpArray)) ? implode($glue, $tmpArray) : false; 
  249.  
  250. public static function attributesToString($attributes) { 
  251. $printableAttribs = []; 
  252. foreach ($attributes AS $key => $value) { 
  253. if (is_array($value)) { 
  254. $pAttrib = self::attributesToString($value); 
  255. } else if ($value instanceof DateTime) { 
  256. $pAttrib = $value->format(DateTime::RFC850); 
  257. } else { 
  258. $pAttrib = $value; 
  259. $printableAttribs[$key] = sprintf('%s', $pAttrib); 
  260. return self::implodeAssociativeArray($printableAttribs); 
  261.  
  262. /** 
  263. * verify user request structure 
  264. * compares the expected signature of a gateway request 
  265. * against the actual structure sent by the user 
  266. * @param array $signature 
  267. * @param array $attributes 
  268. */ 
  269. public static function verifyKeys($signature, $attributes) 
  270. $validKeys = self::_flattenArray($signature); 
  271. $userKeys = self::_flattenUserKeys($attributes); 
  272. $invalidKeys = array_diff($userKeys, $validKeys); 
  273. $invalidKeys = self::_removeWildcardKeys($validKeys, $invalidKeys); 
  274.  
  275. if(!empty($invalidKeys)) { 
  276. asort($invalidKeys); 
  277. $sortedList = join(', ', $invalidKeys); 
  278. throw new InvalidArgumentException('invalid keys: ' . $sortedList); 
  279. /** 
  280. * flattens a numerically indexed nested array to a single level 
  281. * @param array $keys 
  282. * @param string $namespace 
  283. * @return array 
  284. */ 
  285. private static function _flattenArray($keys, $namespace = null) 
  286. $flattenedArray = []; 
  287. foreach($keys AS $key) { 
  288. if(is_array($key)) { 
  289. $theKeys = array_keys($key); 
  290. $theValues = array_values($key); 
  291. $scope = $theKeys[0]; 
  292. $fullKey = empty($namespace) ? $scope : $namespace . '[' . $scope . ']'; 
  293. $flattenedArray = array_merge($flattenedArray, self::_flattenArray($theValues[0], $fullKey)); 
  294. } else { 
  295. $fullKey = empty($namespace) ? $key : $namespace . '[' . $key . ']'; 
  296. $flattenedArray[] = $fullKey; 
  297. sort($flattenedArray); 
  298. return $flattenedArray; 
  299.  
  300. private static function _flattenUserKeys($keys, $namespace = null) 
  301. $flattenedArray = []; 
  302.  
  303. foreach($keys AS $key => $value) { 
  304. $fullKey = empty($namespace) ? $key : $namespace; 
  305. if (!is_numeric($key) && $namespace != null) { 
  306. $fullKey .= '[' . $key . ']'; 
  307. if (is_numeric($key) && is_string($value)) { 
  308. $fullKey .= '[' . $value . ']'; 
  309. if(is_array($value)) { 
  310. $more = self::_flattenUserKeys($value, $fullKey); 
  311. $flattenedArray = array_merge($flattenedArray, $more); 
  312. } else { 
  313. $flattenedArray[] = $fullKey; 
  314. sort($flattenedArray); 
  315. return $flattenedArray; 
  316.  
  317. /** 
  318. * removes wildcard entries from the invalid keys array 
  319. * @param array $validKeys 
  320. * @param <array $invalidKeys 
  321. * @return array 
  322. */ 
  323. private static function _removeWildcardKeys($validKeys, $invalidKeys) 
  324. foreach($validKeys AS $key) { 
  325. if (stristr($key, '[_anyKey_]')) { 
  326. $wildcardKey = str_replace('[_anyKey_]', '', $key); 
  327. foreach ($invalidKeys AS $index => $invalidKey) { 
  328. if (stristr($invalidKey, $wildcardKey)) { 
  329. unset($invalidKeys[$index]); 
  330. return $invalidKeys;