Completed
Push — develop ( 8de61d...0af939 )
by Nate
02:17
created

AbstractTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 60
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

5 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
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\ObjectHelper;
13
use Flipbox\Transform\Helpers\TransformerHelper;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
abstract class AbstractTransformer implements TransformerInterface
20
{
21
    /**
22
     * The normalized includes
23
     *
24
     * @var null|array
25
     */
26
    private $includes;
27
28
    /**
29
     * @param array $config
30
     */
31
    public function __construct(array $config = [])
32
    {
33
        ObjectHelper::configure($this, $config);
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    protected function defineIncludes(): array
40
    {
41
        return [];
42
    }
43
44
    /**
45
     * Returns an array of normalized includes.  It is recommend
46
     * @return array
47
     */
48
    public function getIncludes(): array
49
    {
50
        if ($this->includes === null) {
51
            $this->includes = TransformerHelper::normalizeIncludes(
52
                $this->defineIncludes()
53
            );
54
        }
55
56
        return $this->includes;
57
    }
58
59
    /**
60
     * @param mixed $data
61
     * @param TransformerInterface|callable $transformer
62
     * @return Item
63
     */
64
    protected function item($data, $transformer): Item
65
    {
66
        return new Item(['data' => $data, 'transformer' => $transformer]);
67
    }
68
69
    /**
70
     * @param mixed $data
71
     * @param TransformerInterface|callable $transformer
72
     * @return Collection
73
     */
74
    protected function collection($data, $transformer): Collection
75
    {
76
        return new Collection(['data' => $data, 'transformer' => $transformer]);
77
    }
78
}
79