Completed
Push — master ( c1ef80...f9ae48 )
by Naylon Kessler de
02:39
created

Returning::detailMapper()   A

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
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace SmartCNAB\Support\File;
4
5
use SmartCNAB\Contracts\File\ReturningInterface;
6
use SmartCNAB\Support\Picture;
7
8
/**
9
 * Base returning class.
10
 */
11
class Returning extends File implements ReturningInterface
12
{
13
    /**
14
     * Picture instance.
15
     *
16
     * @var \SmartCNAB\Support\Picture
17
     */
18
    protected $picture;
19
20
    /**
21
     * Parsed schema.
22
     *
23
     * @var array
24
     */
25
    protected $schema;
26
27
    /**
28
     * Initialize and return a new instance.
29
     *
30
     * @param  string  $path  path of returning file
31
     * @param  \SmartCNAB\Support\Picture  $picture
32
     */
33
    public function __construct($path, Picture $picture)
34
    {
35
        $this->picture = $picture;
36
        $this->schema = $this->parseSchema();
37
38
        $this->load($path);
39
    }
40
41
    /**
42
     * Return all return details.
43
     *
44
     * @return array
45
     */
46
    public function details()
47
    {
48
        $details = array_slice($this->lines, 1, count($this->lines) - 2);
49
50
        return array_map([$this, 'detailMapper'], $details);
51
    }
52
53
    /**
54
     * Return the parsed schema.
55
     *
56
     * @return array
57
     */
58
    public function getSchema()
59
    {
60
        return $this->schema;
61
    }
62
63
    /**
64
     * Returns the file header.
65
     *
66
     * @return \StdClass
67
     */
68
    public function header()
69
    {
70
        $data = $this->parseLine($this->lines[0], 'header');
71
72
        return (object) $data;
73
    }
74
75
    /**
76
     * Returns the file trailer.
77
     *
78
     * @return \StdClass
79
     */
80
    public function trailer()
81
    {
82
        $data = $this->parseLine(end($this->lines), 'trailer');
83
84
        return (object) $data;
85
    }
86
87
    /**
88
     * Mapper method for one line parsing.
89
     *
90
     * @param  string  $detail
91
     * @return \StdClass
92
     */
93
    protected function detailMapper($detail)
94
    {
95
        $parsed = (object) $this->parseLine($detail);
96
97
        return $parsed;
98
    }
99
100
    /**
101
     * Create and returns a new line parse mapper using received parameters.
102
     *
103
     * @param  string  $data
104
     * @return Closure
105
     */
106
    protected function getParseMapper($data)
107
    {
108
        return function ($meta) use ($data) {
109
            return $this->picture->from($meta['pic'], $data, $meta);
110
        };
111
    }
112
113
    /**
114
     * Parses a line data received using the schema.
115
     *
116
     * @param  string  $data
117
     * @param  string  $type
118
     * @return array
119
     */
120 View Code Duplication
    protected function parseLine($data, $type = 'detail')
121
    {
122
        $metas = $this->schema[$type];
123
        $fields = array_keys($metas);
124
125
        $parsed = array_map($this->getParseMapper($data), $metas);
126
127
        return array_combine($fields, $parsed);
128
    }
129
}
130