Completed
Push — master ( ac0544...a10425 )
by Nate
11:46 queued 42s
created

TransformerHelper::isTransformerClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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