Completed
Push — master ( 7f9ead...f93f95 )
by Vitalijs
02:00
created

DTOBaseBuilder::validateFieldName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2.0625
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
    /** @property DTOBase $dto */
14
    private $dto;
15
16 40
    public function __construct(DTOBase $dtoBase)
17
    {
18 40
        $this->dto = $dtoBase;
19 40
    }
20
21
    /**
22
     * Build DTO from given type of data
23
     * @param $data
24
     * @throws \InvalidArgumentException
25
     */
26 40
    public function build($data)
27
    {
28 40
        switch (gettype($data)) {
29 40
            case 'array':
30 31
                $this->buildFromArray($data);
31 31
                break;
32 10
            case 'object':
33 3
                $this->buildFromObject($data);
34 3
                break;
35 8
            case 'string':
36 6
                $this->buildFromJson($data);
37 4
                break;
38
            default:
39 2
                throw new \InvalidArgumentException('DTO can be built from array|object|json, "'.gettype($data).'" given.');
40
        }
41 36
    }
42
    
43
    /**
44
     * Restrict internalDTO* fields in data
45
     * @param $fieldName
46
     */
47 34
    private function validateFieldName($fieldName){
48 34
        if (in_array($fieldName, ['internalDTOData', 'internalDTODefault'], true)) {
49
            throw new \InvalidArgumentException('internalDTO* fields are restricted');
50
        }
51 34
    }
52
53
    /**
54
     * Build DTO from provided data
55
     * @param $array
56
     */
57 31
    private function buildFromArray($array)
58
    {
59 31
        foreach ($this->dto->getDefault() as $key => $value) {
60 29
            $this->validateFieldName($key);
61
            
62 29
            if (isset($array[$key])) {
63 13
                $this->dto[$key] = $array[$key];
64
            } else {
65 29
                $this->dto[$key] = $value;
66
            }
67
        }
68 31
    }
69
70
    /**
71
     * Build DTO from provided data
72
     * @param $object
73
     */
74 6
    private function buildFromObject($object)
75
    {
76 6
        foreach ($this->dto->getDefault() as $key => $value) {
77 6
            $this->validateFieldName($key);
78
            
79 6
            if (isset($object->{$key})) {
80 6
                $this->dto[$key] = $object->{$key};
81
            } else {
82 6
                $this->dto[$key] = $value;
83
            }
84
        }
85 6
    }
86
87
    /**
88
     * Try to build from provided string as JSON
89
     * @param string $data
90
     * @throws \InvalidArgumentException
91
     */
92 6
    private function buildFromJson($data)
93
    {
94 6
        $triedToDecodeData = json_decode($data);
95
96 6
        if ($triedToDecodeData !== null) {
97 4
            $this->buildFromObject($triedToDecodeData);
98
        } else {
99 2
            throw new \InvalidArgumentException(
100 2
                'DTO can be built from array|object|json, "'.gettype($data).'" given. Probably tried to pass invalid JSON.'
101
            );
102
        }
103 4
    }
104
}
105