ArrayTransformer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 84
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A defineIncludes() 0 4 1
A getIncludes() 0 10 2
A item() 0 4 1
A collection() 0 4 1
A __invoke() 0 16 1
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\Transformers;
11
12
use Flipbox\Transform\Helpers\ArgumentHelper;
13
use Flipbox\Transform\Helpers\ObjectHelper;
14
use Flipbox\Transform\Helpers\TransformerHelper;
15
use Flipbox\Transform\Resources\Collection;
16
use Flipbox\Transform\Resources\Item;
17
use Flipbox\Transform\Scope;
18
19
/**
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 3.0.0
22
 */
23
abstract class ArrayTransformer implements TransformerInterface
24
{
25
    /**
26
     * The normalized includes
27
     *
28
     * @var null|array
29
     */
30
    private $includes;
31
32
    /**
33
     * @param array $config
34
     */
35
    public function __construct(array $config = [])
36
    {
37
        ObjectHelper::configure($this, $config);
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    protected function defineIncludes(): array
44
    {
45
        return [];
46
    }
47
48
    /**
49
     * Returns an array of normalized includes.  It is recommend
50
     * @return array
51
     */
52
    public function getIncludes(): array
53
    {
54
        if ($this->includes === null) {
55
            $this->includes = TransformerHelper::normalizeIncludes(
56
                $this->defineIncludes()
57
            );
58
        }
59
60
        return $this->includes;
61
    }
62
63
    /**
64
     * @param mixed $data
65
     * @param callable $transformer
66
     * @return Item
67
     */
68
    protected function item($data, $transformer): Item
69
    {
70
        return new Item($data, $transformer);
71
    }
72
73
    /**
74
     * @param mixed $data
75
     * @param callable $transformer
76
     * @return Collection
77
     */
78
    protected function collection($data, $transformer): Collection
79
    {
80
        return new Collection($data, $transformer);
81
    }
82
83
    /**
84
     * The $params consist of all the attributes found on the Class::transform() method.
85
     *
86
     * @param mixed ...$params
87
     * @return array
88
     * @throws \ReflectionException
89
     */
90
    public function __invoke(Scope $scope, ...$params): array
91
    {
92
        // Construct an associative array
93
        $args = ArgumentHelper::mergeCallable(
94
            $this,
95
            $params
96
        );
97
98
        /** @var array $data */
99
        $data = call_user_func_array(
100
            [$this, "transform"],
101
            $args
102
        );
103
104
        return $scope->prepareData($this, $data, null, $args);
105
    }
106
}
107