Completed
Push — master ( 74d8cf...88d0d9 )
by Marcin
02:58
created

Helper::getKeyValues()   B

Complexity

Conditions 9
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

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