Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
20.00% covered (danger)
20.00%
2 / 10
CRAP
17.86% covered (danger)
17.86%
10 / 56
DictionaryFormatter
0.00% covered (danger)
0.00%
0 / 1
20.00% covered (danger)
20.00%
2 / 10
290.26
17.86% covered (danger)
17.86%
10 / 56
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 in
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 out
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 __call
0.00% covered (danger)
0.00%
0 / 1
7.35
54.55% covered (warning)
54.55%
6 / 11
 __callStatic
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 5
 format
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 decompile
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 9
 collect
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 map
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 13
 getFormatters
0.00% covered (danger)
0.00%
0 / 1
20
0.00% covered (danger)
0.00%
0 / 8
1<?php
2
3namespace Qmp\Laravel\DictionaryFormatter;
4
5use Closure;
6use Illuminate\Support\Collection;
7use ReflectionMethod;
8
9class DictionaryFormatter
10{
11    /**
12     * Undocumented variable
13     *
14     * @var Collection
15     */
16    protected $customFormatters;
17
18    /**
19     * Undocumented variable
20     *
21     * @var Collection
22     */
23    protected $defaultRules;
24
25    /**
26     * Undocumented variable
27     *
28     * @var Collection
29     */
30    protected $currentRules;
31
32    /**
33     * Undocumented variable
34     *
35     * @var bool
36     */
37    protected $in = true;
38
39
40    /**
41     * Undocumented function
42     *
43     * @param array $customFormatters
44     * @param array $rules
45     */
46    public function __construct($customFormatters, $defaultRules)
47    {
48        $this->customFormatters = $this->collect($customFormatters); //config('formatters.custom');
49        $this->defaultRules = $this->collect($defaultRules); //config('formatters.rules');
50    }
51
52    /**
53     * Undocumented function
54     *
55     * @param array $array
56     * @return array
57     */
58    public function in($array, $currentRules): array
59    {
60        $this->currentRules = $this->collect($currentRules);
61        return $this->format($array);
62    }
63
64    /**
65     * Undocumented function
66     *
67     * @param array $array
68     * @return void
69     */
70    public function out($array, $currentRules): array
71    {
72        $this->in = false;
73        $this->currentRules = $this->collect($currentRules);
74        return $this->format($array);
75    }
76
77    /**
78     * Undocumented function
79     *
80     * @param string $name
81     * @param array $arguments
82     * @return mixed
83     */
84    public function __call($name, $arguments)
85    {
86        if ($arguments[count($arguments) - 1] === "") {
87            array_pop($arguments);
88        }
89
90        if ($this->customFormatters->contains($name)) {
91            $class = "Qmp\\Laravel\\DictionaryFormatter\\Formatters\\" . ucFirst(strtolower($name));
92            $rm = new ReflectionMethod($class, 'format');
93            if ($rm->isStatic()) {
94                return $class::format(...$arguments);
95            }
96        } else if (function_exists($name)) {
97            $arguments[] = array_shift($arguments);
98            return $name(...$arguments);
99        }
100
101        return $arguments[0];
102    }
103
104    /**
105     * Undocumented function
106     *
107     * @param [type] $name
108     * @param [type] $arguments
109     * @return void
110     */
111    public static function __callStatic($name, $arguments)
112    {
113        $class = "Qmp\\Laravel\\DictionaryFormatter\\Formatters\\" . ucFirst(strtolower($name));
114        $rm = new ReflectionMethod($class, 'format');
115
116        if ($rm->isStatic()) {
117            return $class::format(...$arguments);
118        }
119    }
120
121    /**
122     * Undocumented function
123     *
124     * @param array $array
125     * @param boolean $in
126     * @return array
127     */
128    protected function format($array): array
129    {
130        return $this->collect($array)->map(Closure::fromCallable([$this, 'map']))->toArray(); //array_map([$this, 'map'], $array);
131    }
132
133    /**
134     * Undocumented function
135     *
136     * @param string $string
137     * @return array
138     */
139    protected function decompile(string $formatter): array
140    {
141        $formatter = collect(explode(':', $formatter));
142        $formatterName = $formatter->shift();
143        $params = collect(preg_split('/(?![^)(]*\([^)(]*?\)\)),(?![^\[]*\])/', $formatter->implode('')));
144        $params = $params->map(function ($elem) {
145            if (strpos($elem, '[') === 0) {
146                return json_decode($elem);
147            }
148            return $elem;
149        });
150
151        return [$formatterName, $params];
152    }
153
154    /**
155     * Undocumented function
156     *
157     * @param array|Collection $array
158     * @return Collection
159     */
160    protected function collect($array): Collection
161    {
162        return  is_a($array, Collection::class) ? $array : collect($array);
163    }
164
165    /**
166     * Undocumented function
167     *
168     * @param  $array
169     * @return void
170     */
171    protected function map($array, $fieldType): array
172    {
173        $direction = "formatted_" . ($this->in ? 'in' : 'out');
174
175        return $this->collect($array)->map(function($field) use ($direction, $fieldType){
176            if ($this->defaultRules->has($fieldType)) {
177                $formatters =  $this->getFormatters($fieldType);
178                $value = $formatters->reduce(function ($carry, $formatter) {
179                    [$formatterName, $params] = $this->decompile($formatter);
180                    return $this->{$formatterName}($carry, ...$params);
181                }, $field['raw']);
182                $field[$direction] = $value;
183            } else {
184                $field[$direction] = $field['raw'];
185            }
186
187            unset($field['field_type']);
188
189            return $field;
190
191        })->toArray();
192    }
193
194    /**
195     * Undocumented function
196     *
197     * @param string $dictionaryKey
198     * @return Collection
199     */
200    protected function getFormatters($dictionaryKey): Collection
201    {
202        $formatters = $this->in
203            ? $this->currentRules->where('field_type', $dictionaryKey)->first()['import_formatting'] ?? $this->defaultRules[$dictionaryKey]
204            : $this->currentRules->where('field_type', $dictionaryKey)->first()['export_formatting'] ?? $this->defaultRules[$dictionaryKey];
205
206        return is_a($formatters, Collection::class)
207            ? $formatters
208            : (is_array($formatters)
209                ? collect($formatters)
210                : collect(explode('|', $formatters)));
211    }
212}