Issues (6)

src/Util/Util.php (3 issues)

1
<?php
2
3
namespace Digikraaft\Abokifx\Util;
4
5
use Digikraaft\Abokifx\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 Flutterwave API to the corresponding PHP object.
40
     *
41
     * @param array $resp the response from the Flutterwave API
42
     *
43
     * @return array|object
44
     */
45
    public static function convertArrayToObject($resp)
46
    {
47
        if (! is_array($resp)) {
0 ignored issues
show
The condition is_array($resp) is always true.
Loading history...
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
    public static function validateData(array $expectedKeys, array $data)
73
    {
74
        //check if array keys are empty
75
        if (! array_filter($data)) {
76
            throw new \Digikraaft\Mono\Exceptions\InvalidArgumentException("Please check that all required keys are present and not null");
0 ignored issues
show
The type Digikraaft\Mono\Exceptio...nvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
77
        }
78
        if (! self::in_array_all(
79
            $expectedKeys,
80
            $data
81
        )) {
82
            throw new \Digikraaft\KudaBank\Exceptions\InvalidArgumentException("Please check that all required keys are present");
0 ignored issues
show
The type Digikraaft\KudaBank\Exce...nvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
83
        }
84
    }
85
86
    protected static function in_array_all(array $expectedKeys, array $arrayOfData) : bool
87
    {
88
        return ! array_diff_key(array_flip($expectedKeys), $arrayOfData);
89
    }
90
}
91