Helper::getPropertyAsObject()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

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