Completed
Push — develop ( 2854ec...8c6a12 )
by Nate
01:51
created

TransformerHelper::inInclude()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\TransformerIncludesInterface;
14
use Flipbox\Transform\Transformers\TransformerInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class TransformerHelper
21
{
22
    /**
23
     * @param $transformer
24
     * @return bool
25
     */
26 3
    public static function isTransformer($transformer): bool
27
    {
28 3
        return (is_object($transformer) && ($transformer instanceof Closure)) ||
29 3
            $transformer instanceof TransformerInterface;
30
    }
31
32
    /**
33
     * @param $transformer
34
     * @return bool
35
     */
36
    public static function isTransformerClass($transformer): bool
37
    {
38
        return is_string($transformer) &&
39
            (
40
                is_subclass_of($transformer, TransformerInterface::class) ||
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Flipbox\Transform\Trans...sformerInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
41
                method_exists($transformer, '__invoke') // TODO - REVIEW PUBLIC ACCESS
42
            );
43
    }
44
45
    /**
46
     * @param $transformer
47
     * @return null|callable|TransformerInterface
48
     */
49
    public static function resolve($transformer)
50
    {
51
        if (static::isTransformer($transformer)) {
52
            return $transformer;
53
        }
54
55
        if (static::isTransformerClass($transformer)) {
56
            return new $transformer();
57
        }
58
59
        return null;
60
    }
61
62
    /**
63
     * @param TransformerIncludesInterface $transformer
64
     * @param string $key
65
     * @return bool
66
     */
67
    public static function inInclude(TransformerIncludesInterface $transformer, string $key): bool
68
    {
69
        return self::findInArray($transformer->getIncludes(), $key) !== null;
70
    }
71
72
    /**
73
     * @param array $includes
74
     * @return array
75
     */
76
    public static function normalizeIncludes(array $includes): array
77
    {
78
        foreach ($includes as $k => $v) {
79
            if (is_string($v) && ($pos = strrpos($v, '.')) !== false) {
80
                $v = [substr($v, 0, $pos) => [substr($v, $pos + 1)]];
81
            }
82
83
            // normalize sub-includes
84
            $v = is_array($v) ? static::normalizeIncludes($v) : $v;
85
86
            if (is_numeric($k)) {
87
                unset($includes[$k]);
88
89
                if (is_array($v)) {
90
                    $k = key($v);
91
                    $v = reset($v);
92
                } else {
93
                    $k = $v;
94
                }
95
            }
96
97
            $includes[$k] = $v;
98
99
            if (($pos = strrpos($k, '.')) !== false) {
100
                $includes[substr($k, 0, $pos)] = [substr($k, $pos + 1) => $includes[$k]];
101
                unset($includes[$k]);
102
            }
103
        }
104
105
        return $includes;
106
    }
107
108
    /**
109
     * Retrieves the value of an array element or object property with the given key or property name.
110
     *
111
     * @param $array
112
     * @param $key
113
     * @param null $default
114
     * @return mixed|null
115
     */
116
    private static function findInArray(array $array, string $key, $default = null)
117
    {
118
        if (($pos = strrpos($key, '.')) !== false) {
119
            $array = self::findInArray($array, substr($key, 0, $pos), $default);
120
            $key = substr($key, $pos + 1);
121
        }
122
123
        return is_array($array) && array_key_exists($key, $array) ? $array[$key] : $default;
124
    }
125
}
126