TransformerHelper::resolve()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.6666
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 20
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/restful/license
6
 * @link       https://www.flipboxfactory.com/software/restful/
7
 */
8
9
namespace flipbox\craft\restful\helpers;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use craft\helpers\StringHelper;
14
use flipbox\craft\restful\Restful;
15
use Flipbox\Transform\Helpers\TransformerHelper as BaseTransformerHelper;
16
use yii\base\InvalidConfigException;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class TransformerHelper extends BaseTransformerHelper
23
{
24
    /**
25
     * @param $name
26
     * @return string
27
     */
28
    public static function assembleIdentifier($name)
29
    {
30
        if (!is_array($name)) {
31
            $name = ArrayHelper::toArray($name);
32
        }
33
34
        // Clean
35
        $name = array_filter($name);
36
37
        return StringHelper::toLowerCase(
38
            StringHelper::toString($name, ':')
39
        );
40
    }
41
42
    /**
43
     * @param $transformer
44
     * @return bool
45
     */
46
    public static function isTransformerConfig($transformer)
47
    {
48
        if (!is_array($transformer)) {
49
            return false;
50
        }
51
52
        if (null === ($class = ArrayHelper::getValue($transformer, 'class'))) {
53
            return false;
54
        }
55
56
        return static::isTransformerClass($class);
57
    }
58
59
    /**
60
     * @param $transformer
61
     * @return null|callable
62
     */
63
    public static function resolve($transformer)
64
    {
65
        if (null !== ($callable = parent::resolve($transformer))) {
66
            return $callable;
67
        }
68
69
        try {
70
            if (static::isTransformerConfig($transformer)) {
71
                return static::resolve(
72
                    Craft::createObject($transformer)
73
                );
74
            }
75
        } catch (InvalidConfigException $e) {
76
            Restful::warning("Invalid transformer configuration.");
77
        }
78
79
        return null;
80
    }
81
}
82