Completed
Push — develop ( 1afc6f...41be06 )
by Nate
01:55
created

TransformerHelper::isTransformerClass()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

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