Completed
Push — master ( 8b1d39...e6cddd )
by Jakob
01:31
created

Constructor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
setField() 0 1 ?
B __construct() 0 13 5
1
<?php declare(strict_types = 1);
2
3
namespace JSKOS;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Initialize field values of a new JSKOS Object.
9
 */
10
trait Constructor
11
{
12
    /**
13
     * Set a field value.
14
     */
15
    abstract protected function setField(string $field, $value, bool $strict = true);
16
17
    /**
18
     * Create a new JSKOS object with given field values.
19
     *
20
     * @param Array|Object fields to copy
21
     * @param bool strict throw error on unknown fields
22
     */
23
    public function __construct($data = null, bool $strict = false)
24
    {
25
        if (is_array($data) || is_object($data)) {
26
            foreach ($data as $key => $value) {
27
                $this->setField($key, $value, $strict);
28
            }
29
        } elseif ($data !== null) {
30
            throw new InvalidArgumentException(
31
                get_called_class() .
32
                ' constructor expects array, object, or null'
33
            );
34
        }
35
    }
36
}
37