Total Complexity | 5 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Coverage | 76.92% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
7 | trait FromArray |
||
8 | { |
||
9 | /** |
||
10 | * @param array $array |
||
11 | * |
||
12 | * @throws MethodDoesNotExistsException |
||
13 | * |
||
14 | * @return static |
||
15 | */ |
||
16 | 3 | public static function fromArray(array $array): self |
|
17 | { |
||
18 | 3 | $self = new static(); |
|
19 | |||
20 | 3 | foreach ($array as $property => $value) { |
|
21 | 3 | $method = $self->setMethod($property); |
|
22 | $self->$method($value); |
||
23 | } |
||
24 | |||
25 | return $self; |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @param string $property |
||
30 | * |
||
31 | * @throws MethodDoesNotExistsException |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | 3 | private function setMethod(string $property): string |
|
36 | { |
||
37 | 3 | $method = $this->snakeCaseToCamelCase('set'.$property); |
|
38 | |||
39 | 3 | if (! method_exists($this, $method)) { |
|
40 | 3 | throw new MethodDoesNotExistsException("Method {$method} does not exists."); |
|
41 | } |
||
42 | |||
43 | return $method; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param string $string |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 3 | private function snakeCaseToCamelCase(string $string): string |
|
54 | } |
||
55 | } |
||
56 |