Parameterizable::fillFromParameters()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
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