| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright © 2017 SeQura Engineering. All rights reserved. |
| 4 |
*/ |
| 5 |
|
| 6 |
|
| 7 |
namespace Sequra\PhpClient; |
| 8 |
|
| 9 |
class Helper |
| 10 |
{ |
| 11 |
const ISO8601_PATTERN = '^((\d{4})-([0-1]\d)-([0-3]\d))+$|P(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$'; |
| 12 |
|
| 13 |
public static function isConsistentCart($cart) |
| 14 |
{ |
| 15 |
$totals = self::totals($cart); |
| 16 |
return $cart['order_total_with_tax'] == $totals['with_tax']; |
| 17 |
} |
| 18 |
|
| 19 |
public static function totals($cart) |
| 20 |
{ |
| 21 |
$total_with_tax = 0; |
| 22 |
foreach ($cart['items'] as $item) { |
| 23 |
$total_with_tax += $item['total_with_tax']; |
| 24 |
} |
| 25 |
|
| 26 |
return array('with_tax' => $total_with_tax); |
| 27 |
} |
| 28 |
|
| 29 |
public static function removeNulls($data) |
| 30 |
{ |
| 31 |
foreach ($data as $key => $value) { |
| 32 |
if (is_null($value)) { |
| 33 |
unset($data[$key]); |
| 34 |
} else { |
| 35 |
if (is_array($value)) { |
| 36 |
$data[$key] = self::removeNulls($value); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
return $data; |
| 42 |
} |
| 43 |
|
| 44 |
public static function removeHtmlEntities($data) |
| 45 |
{ |
| 46 |
foreach ($data as $key => $value) { |
| 47 |
if (is_string($value)) { |
| 48 |
$data[$key] = html_entity_decode($value, ENT_COMPAT | ENT_HTML401, 'UTF-8'); |
| 49 |
} else { |
| 50 |
if (is_array($value)) { |
| 51 |
$data[$key] = self::removeHtmlEntities($value); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
return $data; |
| 56 |
} |
| 57 |
|
| 58 |
public static function notNull($value1, $value2) |
| 59 |
{ |
| 60 |
return is_null($value1) ? $value2 : $value1; |
| 61 |
} |
| 62 |
|
| 63 |
public static function sign($value, $pass) |
| 64 |
{ |
| 65 |
$signature = hash_hmac('sha256', $value, $pass); |
| 66 |
if ($signature) { |
| 67 |
return $signature; |
| 68 |
} |
| 69 |
|
| 70 |
return sha1($value . $pass); |
| 71 |
} |
| 72 |
} |
| 73 |
|