PregReplaceKeys   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 5 1
B transform() 0 7 5
1
<?php
2
3
namespace Ghc\Rosetta\Transformers;
4
5
class PregReplaceKeys extends Transformer
6
{
7
    /**
8
     * Boot Transformer.
9
     */
10
    protected function boot()
11
    {
12
        $this->addDefaultConfig([
13
            'pattern'     => '//',
14
            'replacement' => '',
15
        ]);
16
    }
17
18
    /**
19
     * @param array $data
20
     *
21
     * @return array
22
     */
23
    public function transform($data)
24
    {
25
        return collect($data)->mapWithKeys(function ($value, $key) {
26
            if (!count($this->config['properties']) || (count($this->config['properties']) && in_array($key, $this->config['properties']))) {
27
                return [preg_replace($this->config['pattern'], $this->config['replacement'], $key) => !is_array($value) ? $value : $this->transform($value)];
28
            }
29
        })->toArray();
30
    }
31
}
32