DataTransferObjectTrait::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
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 9
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
//----------------------------------------------------------------------
4
//
5
//  Copyright (C) 2017-2020 Artem Rodygin
6
//
7
//  You should have received a copy of the MIT License along with
8
//  this file. If not, see <http://opensource.org/licenses/MIT>.
9
//
10
//----------------------------------------------------------------------
11
12
namespace Webinarium;
13
14
/**
15
 * Trait to initialize Data Transfer Objects right on creation.
16
 */
17
trait DataTransferObjectTrait
18
{
19
    /**
20
     * Initializes object properties with values from provided array.
21
     *
22
     * @param array $values Initial values.
23
     */
24 2
    public function __construct(?array $values = null)
25
    {
26 2
        $data = $values ?? [];
27
28 2
        $properties = array_keys(get_class_vars(static::class));
29
30 2
        foreach ($properties as $property) {
31 2
            if (array_key_exists($property, $data)) {
32 1
                $this->{$property} = $data[$property];
33
            }
34
        }
35 2
    }
36
}
37