Item::pipe()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Ghc\Rosetta;
4
5
use Ghc\Rosetta\Pipes\Pipeable;
6
use Ghc\Rosetta\Transformers\Skip;
7
use Ghc\Rosetta\Transformers\Transformer;
8
use Illuminate\Contracts\Support\Arrayable;
9
10
class Item implements Arrayable, Pipeable
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $data;
16
17
    /**
18
     * @var Transformer[]
19
     */
20
    protected $transformers;
21
22
    /**
23
     * Item constructor.
24
     *
25
     * @param array|mixed               $data
26
     * @param Transformer|Transformer[] $transformers
27
     */
28
    public function __construct($data = [], $transformers = null)
29
    {
30
        $this->setData($data);
31
        $this->setTransformers($transformers);
32
    }
33
34
    /**
35
     * @return array
36
     */
37
    public function getData()
38
    {
39
        return $this->data;
40
    }
41
42
    /**
43
     * @param array|mixed $data
44
     *
45
     * @return self
46
     */
47
    public function setData($data)
48
    {
49
        $this->data = (array) $data;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @return Transformers\Transformer[]
56
     */
57
    public function getTransformers()
58
    {
59
        return $this->transformers;
60
    }
61
62
    /**
63
     * @param Transformers\Transformer|Transformers\Transformer[] $transformers
64
     *
65
     * @return self
66
     */
67
    public function setTransformers($transformers)
68
    {
69
        $transformers = $transformers ?: new Skip();
70
        $this->transformers = is_array($transformers) ? $transformers : [$transformers];
71
72
        return $this;
73
    }
74
75
    /**
76
     * Get the instance as an array.
77
     *
78
     * @return array
79
     */
80
    public function toArray()
81
    {
82
        $data = $this->getData();
83
84
        foreach ($this->transformers as $transformer) {
85
            $data = $transformer->transform($data);
86
        }
87
88
        return $data;
89
    }
90
91
    /**
92
     * @param array $options
93
     *
94
     * @return \Closure
95
     */
96
    public function pipe($options = [])
97
    {
98
        return function ($inputData) use ($options) {
0 ignored issues
show
Unused Code introduced by
The import $options is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
99
            $this->setData($inputData);
100
101
            return $this->toArray();
102
        };
103
    }
104
}
105