1 | <?php |
||
2 | |||
3 | namespace Digikraaft\Paystack\Util; |
||
4 | |||
5 | use Digikraaft\Paystack\Exceptions\InvalidArgumentException; |
||
6 | use stdClass; |
||
7 | |||
8 | abstract class Util |
||
9 | { |
||
10 | private static $isMbstringAvailable = null; |
||
11 | private static $isHashEqualsAvailable = null; |
||
12 | |||
13 | /** |
||
14 | * @param mixed|string $value a string to UTF8-encode |
||
15 | * |
||
16 | * @return mixed|string the UTF8-encoded string, or the object passed in if |
||
17 | * it wasn't a string |
||
18 | */ |
||
19 | public static function utf8($value) |
||
20 | { |
||
21 | if (null === self::$isMbstringAvailable) { |
||
22 | self::$isMbstringAvailable = function_exists('mb_detect_encoding'); |
||
23 | |||
24 | if (! self::$isMbstringAvailable) { |
||
25 | trigger_error('It looks like the mbstring extension is not enabled. '. |
||
26 | 'UTF-8 strings will not properly be encoded. Ask your system '. |
||
27 | 'administrator to enable the mbstring extension.', E_USER_WARNING); |
||
28 | } |
||
29 | } |
||
30 | |||
31 | if (is_string($value) && self::$isMbstringAvailable && 'UTF-8' !== mb_detect_encoding($value, 'UTF-8', true)) { |
||
32 | return utf8_encode($value); |
||
33 | } |
||
34 | |||
35 | return $value; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Converts a response from the Paystack API to the corresponding PHP object. |
||
40 | * |
||
41 | * @param array $resp the response from the Paystack API |
||
42 | * |
||
43 | * @return array|object |
||
44 | */ |
||
45 | public static function convertArrayToObject($resp) |
||
46 | { |
||
47 | if (! is_array($resp)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
48 | $message = 'The response passed must be an array'; |
||
49 | |||
50 | throw new InvalidArgumentException($message); |
||
51 | } |
||
52 | |||
53 | $object = new stdClass(); |
||
54 | |||
55 | return self::arrayToObject($resp, $object); |
||
56 | } |
||
57 | |||
58 | private static function arrayToObject($array, &$obj) |
||
59 | { |
||
60 | foreach ($array as $key => $value) { |
||
61 | if (is_array($value)) { |
||
62 | $obj->$key = new stdClass(); |
||
63 | self::arrayToObject($value, $obj->$key); |
||
64 | } else { |
||
65 | $obj->$key = $value; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | return $obj; |
||
70 | } |
||
71 | } |
||
72 |