Passed
Push — master ( 6d3818...32c328 )
by Valentin
04:20
created

ParameterManipulations::fillWithSnakeParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
4
namespace Pheanstalk\Structure;
5
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
9
class ParameterManipulations
10
{
11
    /**
12
     * ParameterManipulations constructor.
13
     *
14
     * @param array $params
15
     *
16
     * @throws \ReflectionException
17
     */
18 36
    public function __construct(array $params)
19
    {
20 36
        $this->fillWithSnakeParams($params);
21
    }
22
23
    /**
24
     * @param        $input
25
     * @param string $glue
26
     *
27
     * @return string
28
     */
29 40
    protected function fromCamelCase($input, $glue = '_')
30
    {
31 40
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
32 40
        $ret = $matches[0];
33 40
        foreach ($ret as &$match) {
34 40
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
35
        }
36 40
        return implode($glue, $ret);
37
    }
38
39 36
    public function fillWithSnakeParams(array $params, $glue = '_')
40
    {
41 36
        $thisObject = new \ReflectionClass($this);
42 36
        $properties = $thisObject->getProperties();
43 36
        foreach ($properties as $property) {
44 36
            $snakeProperty = $this->fromCamelCase($property->getName(), $glue);
45 36
            if (isset($params[$snakeProperty])) {
46 19
                $this->{$property->getName()} = $params[$snakeProperty];
47
            }
48
        }
49
    }
50
}