TransformerHelper   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 47.92%

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 1
dl 0
loc 149
ccs 23
cts 48
cp 0.4792
rs 9.84
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isClosure() 0 4 2
A isInvokable() 0 6 3
A isCallableArray() 0 4 2
A isTransformer() 0 4 2
A isTransformable() 0 7 3
A isTransformerClass() 0 9 4
A resolve() 0 12 3
A inInclude() 0 4 1
B normalizeIncludes() 0 31 8
A findInArray() 0 9 4
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/transform/releases/latest
7
 * @license   https://github.com/flipbox/transform/blob/master/LICENSE
8
 */
9
10
namespace Flipbox\Transform\Helpers;
11
12
use Closure;
13
use Flipbox\Transform\Transformers\TransformerInterface;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 3.0.0
18
 */
19
class TransformerHelper
20
{
21
    /**
22
     * @param $transformer
23
     * @return bool
24
     */
25 6
    public static function isClosure($transformer): bool
26
    {
27 6
        return is_object($transformer) && $transformer instanceof Closure;
28
    }
29
30
    /**
31
     * @param $transformer
32
     * @return bool
33
     */
34 6
    public static function isInvokable($transformer): bool
35
    {
36 6
        return is_object($transformer) &&
37 6
            is_callable($transformer) &&
38 6
            !$transformer instanceof Closure;
39
    }
40
41
    /**
42
     * @param $transformer
43
     * @return bool
44
     */
45 6
    public static function isCallableArray($transformer): bool
46
    {
47 6
        return is_array($transformer) && is_callable($transformer);
48
    }
49
50
    /**
51
     * @param $transformer
52
     * @return bool
53
     *
54
     * @deprecated Use TransformerHelper::isTransformable()
55
     */
56
    public static function isTransformer($transformer): bool
57
    {
58
        return static::isClosure($transformer) || static::isInvokable($transformer);
59
    }
60
61
    /**
62
     * @param $transformer
63
     * @return bool
64
     */
65 3
    public static function isTransformable($transformer): bool
66
    {
67
        return
68 3
            static::isClosure($transformer) ||
69 3
            static::isInvokable($transformer) ||
70 3
            static::isCallableArray($transformer);
71
    }
72
73
    /**
74
     * @param $transformer
75
     * @return bool
76
     */
77 6
    public static function isTransformerClass($transformer): bool
78
    {
79 6
        return is_string($transformer) &&
80 6
            class_exists($transformer) &&
81
            (
82 6
                method_exists($transformer, '__invoke') ||
83 6
                is_callable([$transformer, '__invoke'])
84
            );
85
    }
86
87
    /**
88
     * @param $transformer
89
     * @return null|callable
90
     */
91 3
    public static function resolve($transformer)
92
    {
93 3
        if (is_callable($transformer)) {
94 3
            return $transformer;
95
        }
96
97 3
        if (static::isTransformerClass($transformer)) {
98 3
            return new $transformer();
99
        }
100
101 3
        return null;
102
    }
103
104
    /**
105
     * @param TransformerInterface $transformer
106
     * @param string $key
107
     * @return bool
108
     */
109
    public static function inInclude(TransformerInterface $transformer, string $key): bool
110
    {
111
        return self::findInArray($transformer->getIncludes(), $key) !== null;
112
    }
113
114
    /**
115
     * @param array $includes
116
     * @return array
117
     */
118
    public static function normalizeIncludes(array $includes): array
119
    {
120
        foreach ($includes as $k => $v) {
121
            if (is_string($v) && ($pos = strrpos($v, '.')) !== false) {
122
                $v = [substr($v, 0, $pos) => [substr($v, $pos + 1)]];
123
            }
124
125
            // normalize sub-includes
126
            $v = is_array($v) ? static::normalizeIncludes($v) : $v;
127
128
            if (is_numeric($k)) {
129
                unset($includes[$k]);
130
131
                if (is_array($v)) {
132
                    $k = key($v);
133
                    $v = reset($v);
134
                } else {
135
                    $k = $v;
136
                }
137
            }
138
139
            $includes[$k] = $v;
140
141
            if (($pos = strrpos($k, '.')) !== false) {
142
                $includes[substr($k, 0, $pos)] = [substr($k, $pos + 1) => $includes[$k]];
143
                unset($includes[$k]);
144
            }
145
        }
146
147
        return $includes;
148
    }
149
150
    /**
151
     * Retrieves the value of an array element or object property with the given key or property name.
152
     *
153
     * @param $array
154
     * @param $key
155
     * @param null $default
156
     * @return mixed|null
157
     */
158
    private static function findInArray(array $array, string $key, $default = null)
159
    {
160
        if (($pos = strrpos($key, '.')) !== false) {
161
            $array = self::findInArray($array, substr($key, 0, $pos), $default);
162
            $key = substr($key, $pos + 1);
163
        }
164
165
        return is_array($array) && array_key_exists($key, $array) ? $array[$key] : $default;
166
    }
167
}
168