Completed
Push — master ( 84750e...bb7498 )
by Marcin
02:41
created

Helper::convertToBoolean()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
/**
3
 * TERYT-API
4
 *
5
 * Copyright (c) 2017 pudelek.org.pl
6
 *
7
 * For the full copyright and license information, please view source file
8
 * that is bundled with this package in the file LICENSE
9
 *
10
 * Author Marcin Pudełek <[email protected]>
11
 *
12
 */
13
14
namespace mrcnpdlk\Teryt;
15
16
17
use mrcnpdlk\Teryt\Exception\Connection;
18
19
/**
20
 * Class Helper
21
 *
22
 * @package mrcnpdlk\Teryt
23
 */
24
class Helper
25
{
26
    /**
27
     * Converting value to boolean
28
     *
29
     * @param $exclude
30
     *
31
     * @return boolean
32
     */
33
    public static function convertToBoolean($exclude)
34
    {
35
        if (is_bool($exclude)) {
36
            return $exclude;
37
        } else {
38
            if (is_numeric($exclude)) {
39
                return $exclude === 1;
40
            } else {
41
                if (is_string($exclude)) {
42
                    return strtolower(trim($exclude)) === 'true';
43
                } else {
44
                    return false;
45
                }
46
            }
47
        }
48
    }
49
50
51
    /**
52
     * Catching and managment of exceptions
53
     *
54
     * @param \Exception $e
55
     *
56
     * @return \Exception|Exception|Connection
57
     */
58
    public static function handleException(\Exception $e)
59
    {
60
        if ($e instanceof \SoapFault) {
61
            switch ($e->faultcode ?? null) {
0 ignored issues
show
Bug introduced by
The property faultcode does not seem to exist on SoapFault.
Loading history...
62
                case 'a:InvalidSecurityToken':
63
                    return new Connection(sprintf('Invalid Security Token'), 1, $e);
64
                case 'WSDL':
65
                    return new Connection(sprintf('%s', $e->faultstring ?? 'Unknown'), 2, $e);
0 ignored issues
show
Bug introduced by
The property faultstring does not exist on SoapFault. Did you mean string?
Loading history...
66
                case 'Client':
67
                    return new Connection(sprintf('%s', $e->faultstring ?? 'Unknown'), 3, $e);
68
                default:
69
                    return new Connection(sprintf('%s', 'Unknown'), 99, $e);
70
            }
71
        } else {
72
            if ($e instanceof Exception) {
73
                return $e;
74
            } else {
75
                return new Exception('Unknown Exception', 1, $e);
76
            }
77
        }
78
    }
79
80
    /**
81
     * Return unique property/key values from array of object/array
82
     *
83
     * @param array  $tItems
84
     * @param string $sKey
85
     * @param bool   $asUnique
86
     *
87
     * @return array
88
     * @throws Exception
89
     */
90
    public static function getKeyValues(array $tItems, string $sKey, bool $asUnique = true)
91
    {
92
        $answer = [];
93
        foreach ($tItems as $item) {
94
            if (!is_object($item) && !is_array($item)) {
95
                throw new Exception(sprintf('%s() elem of tInput is not an array|object, is %s', __METHOD__, gettype($item)));
96
            }
97
            $item = (array)$item;
98
            if (
99
                array_key_exists($sKey, $item)
100
                && (is_string($item[$sKey]) || is_numeric($item[$sKey]))
101
                && !in_array($item[$sKey], $answer)
102
            ) {
103
                $answer[] = $item[$sKey];
104
            }
105
        }
106
107
        return $asUnique ? array_unique($answer) : $answer;
108
    }
109
110
    /**
111
     * Fixing Teryt API bug
112
     *
113
     * If only one item exists in response, returned property is not a array but object type
114
     *
115
     * @param \stdClass $oObject
116
     * @param string    $sPropertyName
117
     *
118
     * @return array
119
     * @throws Exception
120
     */
121
    public static function getPropertyAsArray(\stdClass $oObject, string $sPropertyName)
122
    {
123
        if (!property_exists($oObject, $sPropertyName)) {
124
            throw new Exception\NotFound(sprintf('%s() Property [%s] not exist in object', __METHOD__, $sPropertyName));
125
        }
126
        if (!is_array($oObject->{$sPropertyName})) {
127
            return [$oObject->{$sPropertyName}];
128
        } else {
129
            return $oObject->{$sPropertyName};
130
        }
131
    }
132
133
    /**
134
     * Fixing Teryt API bug
135
     *
136
     * If only one item exists in response, returned property is not a array but object type
137
     *
138
     * @param \stdClass $oObject
139
     * @param string    $sPropertyName
140
     *
141
     * @return mixed
142
     * @throws Exception
143
     * @throws Exception\NotFound
144
     */
145
    public static function getPropertyAsObject(\stdClass $oObject, string $sPropertyName)
146
    {
147
        if (!property_exists($oObject, $sPropertyName)) {
148
            throw new Exception\NotFound(sprintf('%s() Property [%s] not exist in object', __METHOD__, $sPropertyName));
149
        }
150
        if (!is_object($oObject->{$sPropertyName})) {
151
            throw new Exception(sprintf('%s() Property [%s] is not an object type [is:%s]', __METHOD__, $sPropertyName,
152
                gettype($oObject->{$sPropertyName})));
153
        } else {
154
            return $oObject->{$sPropertyName};
155
        }
156
    }
157
}
158