AbstractTransformer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 3.0.0
17
 */
18
abstract class AbstractTransformer
19
{
20
    /**
21
     * @param array $config
22
     */
23 18
    public function __construct(array $config = [])
24
    {
25 18
        ObjectHelper::configure($this, $config);
26 18
    }
27
28
    /**
29
     * @param mixed $data
30
     * @param callable $transformer
31
     * @param array $extra
32
     * @return mixed
33
     */
34
    protected function item($data, callable $transformer, array $extra = [])
35
    {
36
        return call_user_func_array(
37
            $transformer,
38
            array_merge(
39
                [$data],
40
                $extra
41
            )
42
        );
43
    }
44
45
    /**
46
     * @param mixed $data
47
     * @param callable $transformer
48
     * @param array $extra
49
     * @return mixed
50
     */
51
    protected function collection($data, callable $transformer, array $extra = [])
52
    {
53
        $items = [];
54
        foreach ($data as $item) {
55
            $items[] = $this->item($item, $transformer, $extra);
56
        }
57
        return $items;
58
    }
59
}
60