Completed
Push — master ( 6986c5...be73e7 )
by Vitalijs
02:50
created

DTOBaseBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: vlitvinovs
5
 * Date: 2/8/17
6
 * Time: 4:28 PM
7
 */
8
9
namespace CodinPro\DataTransferObject;
10
11
class DTOBaseBuilder
12
{
13 40
    public function __construct(DTOBase $dtoBase)
14
    {
15 40
        $this->dto = $dtoBase;
0 ignored issues
show
Bug introduced by
The property dto does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16 40
    }
17
18
    /**
19
     * Build DTO from given type of data
20
     * @param $data
21
     * @throws \InvalidArgumentException
22
     */
23 40
    public function build($data)
24
    {
25 40
        switch (gettype($data)) {
26 40
            case 'array':
27 10
            case 'object':
28 33
                $this->buildFromData($data);
29 33
                break;
30 8
            case 'string':
31 6
                $this->buildFromJson($data);
32 4
                break;
33
            default:
34 2
                throw new \InvalidArgumentException('DTO can be built from array|object|json, "'.gettype($data).'" given.');
35
        }
36 36
    }
37
38
    /**
39
     * Build DTO from provided data
40
     * @param object|array $data
41
     */
42 36
    private function buildFromData($data)
43
    {
44 36
        foreach ($this->dto->getDefault() as $key => $value) {
45 34
            if (is_object($data) && isset($data->{$key})) {
46 6
                $this->dto[$key] = $data->{$key};
47 34
            } else if (is_array($data) && isset($data[$key])) {
48 13
                $this->dto[$key] = $data[$key];
49
            } else {
50 34
                $this->dto[$key] = $value;
51
            }
52
        }
53 36
    }
54
55
    /**
56
     * Try to build from provided string as JSON
57
     * @param string $data
58
     * @throws \InvalidArgumentException
59
     */
60 6
    private function buildFromJson($data)
61
    {
62 6
        $triedToDecodeData = json_decode($data);
63
64 6
        if ($triedToDecodeData !== null) {
65 4
            $this->buildFromData($triedToDecodeData);
66
        } else {
67 2
            throw new \InvalidArgumentException(
68 2
                'DTO can be built from array|object|json, "'.gettype($data).'" given. Probably tried to pass invalid JSON.'
69
            );
70
        }
71
    }
72
}