Completed
Push — master ( ce5be5...8c5264 )
by Nate
01:51 queued 48s
created

TransformerHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 60
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assembleIdentifier() 0 13 2
A isTransformerConfig() 0 12 3
A resolve() 0 18 4
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