Parameterizable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 26
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fillFromParameters() 0 8 3
A toCamel() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kafkiansky\SmsRu;
6
7
trait Parameterizable
8
{
9
    /**
10
     * @param array $parameters
11
     */
12
    protected function fillFromParameters(array $parameters = []): void
13
    {
14
        foreach ($parameters as $property => $value) {
15
            if (!\property_exists($this, $camel = $this->toCamel($property))) {
16
                throw new \RuntimeException(\sprintf('Property %s for class %s doesn\'t exists', $property, \get_class($this)));
17
            }
18
19
            $this->$camel = $value;
20
        }
21
    }
22
23
    /**
24
     * @param string $property
25
     *
26
     * @return string
27
     */
28
    private function toCamel(string $property): string
29
    {
30
        $property = \ucwords(\str_replace(['-', '_'], ' ', $property));
31
32
        return \lcfirst(\str_replace(' ', '', $property));
33
    }
34
}
35