Completed
Push — master ( d0cc87...6d3818 )
by Valentin
03:32
created

ParameterManipulations   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 34
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A fromCamelCase() 0 8 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
        $thisObject = new \ReflectionClass($this);
21 36
        $properties = $thisObject->getProperties();
22 36
        foreach ($properties as $property) {
23 36
            $snakeProperty = $this->fromCamelCase($property->getName());
24 36
            if (isset($params[$snakeProperty])) {
25 19
                $this->{$property->getName()} = $params[$snakeProperty];
26
            }
27
        }
28
    }
29
30
    /**
31
     * @param $input
32
     *
33
     * @return string
34
     */
35 36
    protected function fromCamelCase($input)
36
    {
37 36
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
38 36
        $ret = $matches[0];
39 36
        foreach ($ret as &$match) {
40 36
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
41
        }
42 36
        return implode('_', $ret);
43
    }
44
}