Completed
Push — develop ( a379bf )
by Nate
01:48
created

TransformerHelper   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 15.38%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 122
ccs 6
cts 39
cp 0.1538
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A isClosure() 0 4 1
A isCallable() 0 4 2
A isTransformer() 0 4 2
A isTransformerClass() 0 7 3
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 3
    public static function isClosure($transformer): bool
26
    {
27 3
        return $transformer instanceof Closure;
28
    }
29
30
    /**
31
     * @param $transformer
32
     * @return bool
33
     */
34 3
    public static function isCallable($transformer): bool
35
    {
36 3
        return is_object($transformer) && is_callable($transformer);
37
    }
38
39
    /**
40
     * @param $transformer
41
     * @return bool
42
     */
43 3
    public static function isTransformer($transformer): bool
44
    {
45 3
        return static::isClosure($transformer) || static::isCallable($transformer);
46
    }
47
48
    /**
49
     * @param $transformer
50
     * @return bool
51
     */
52
    public static function isTransformerClass($transformer): bool
53
    {
54
        return is_string($transformer) && (
55
                is_subclass_of($transformer, TransformerInterface::class) ||
56
                is_callable($transformer, '__invoke')
57
            );
58
    }
59
60
    /**
61
     * @param $transformer
62
     * @return null|callable|TransformerInterface
63
     */
64
    public static function resolve($transformer)
65
    {
66
        if (static::isTransformer($transformer)) {
67
            return $transformer;
68
        }
69
70
        if (static::isTransformerClass($transformer)) {
71
            return new $transformer();
72
        }
73
74
        return null;
75
    }
76
77
    /**
78
     * @param TransformerInterface $transformer
79
     * @param string $key
80
     * @return bool
81
     */
82
    public static function inInclude(TransformerInterface $transformer, string $key): bool
83
    {
84
        return self::findInArray($transformer->getIncludes(), $key) !== null;
85
    }
86
87
    /**
88
     * @param array $includes
89
     * @return array
90
     */
91
    public static function normalizeIncludes(array $includes): array
92
    {
93
        foreach ($includes as $k => $v) {
94
            if (is_string($v) && ($pos = strrpos($v, '.')) !== false) {
95
                $v = [substr($v, 0, $pos) => [substr($v, $pos + 1)]];
96
            }
97
98
            // normalize sub-includes
99
            $v = is_array($v) ? static::normalizeIncludes($v) : $v;
100
101
            if (is_numeric($k)) {
102
                unset($includes[$k]);
103
104
                if (is_array($v)) {
105
                    $k = key($v);
106
                    $v = reset($v);
107
                } else {
108
                    $k = $v;
109
                }
110
            }
111
112
            $includes[$k] = $v;
113
114
            if (($pos = strrpos($k, '.')) !== false) {
115
                $includes[substr($k, 0, $pos)] = [substr($k, $pos + 1) => $includes[$k]];
116
                unset($includes[$k]);
117
            }
118
        }
119
120
        return $includes;
121
    }
122
123
    /**
124
     * Retrieves the value of an array element or object property with the given key or property name.
125
     *
126
     * @param $array
127
     * @param $key
128
     * @param null $default
129
     * @return mixed|null
130
     */
131
    private static function findInArray(array $array, string $key, $default = null)
132
    {
133
        if (($pos = strrpos($key, '.')) !== false) {
134
            $array = self::findInArray($array, substr($key, 0, $pos), $default);
135
            $key = substr($key, $pos + 1);
136
        }
137
138
        return is_array($array) && array_key_exists($key, $array) ? $array[$key] : $default;
139
    }
140
}
141