Completed
Pull Request — master (#445)
by
unknown
02:19
created

ParamHelpers   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanParams() 0 8 2
A cleanValueFrom() 0 7 2
1
<?php
2
3
namespace Mpociot\ApiDoc\Tools\Traits;
4
5
trait ParamHelpers {
6
7
    /**
8
     * Create proper arrays from dot-noted parameter names
9
     *
10
     * @param array $params
11
     * @return array
12
     */
13
    protected function cleanParams(array $params)
14
    {
15
        $values = [];
16
        foreach ($params as $name => $details) {
17
            $this->cleanValueFrom($name, $details['value'], $values);
18
        }
19
        return $values;
20
    }
21
22
    /**
23
     * Converts dot notation names to arrays and sets the value at the right depth
24
     *
25
     * @param string $name
26
     * @param mixed $value
27
     * @param array $values The array that holds the result
28
     * @return void
29
     */
30
    protected function cleanValueFrom($name, $value, array &$values = [])
31
    {
32
        if (str_contains($name, '[')) {
33
            $name = str_replace(['][', '[', ']', '..'], ['.', '.', '', '.*.'], $name);
34
        }
35
        array_set($values, str_replace('.*', '.0', $name), $value);
36
    }
37
}
38