PregReplaceKeys::transform()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 4
nc 1
nop 1
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