ParameterManipulations::fromCamelCase()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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