Completed
Push — develop ( d56c86...d3d8e2 )
by Nate
06:51
created

DynamicModelError::populateSource()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\hubspot\transformers;
10
11
use flipbox\hubspot\transformers\error\Interpret;
12
use Flipbox\Transform\Factory;
13
use yii\base\DynamicModel;
14
15
/**
16
 * This transformer will take an API response and create/populate a User element.
17
 *
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.0
20
 */
21
class DynamicModelError
22
{
23
    /**
24
     * @param array $data
25
     * @return mixed
26
     */
27
    public function __invoke(array $data)
28
    {
29
        $errors = $this->transformErrors($data);
30
31
        $model = new DynamicModel();
32
        $model->addErrors($errors);
33
34
        return $model;
35
    }
36
37
    /**
38
     * @param array $data
39
     * @return array
40
     */
41
    protected function transformErrors(array $data): array
42
    {
43
        $errors = Factory::item(
44
            new Interpret,
45
            $data
46
        );
47
48
        if (!$errors) {
49
            $errors = [$errors];
50
        }
51
52
        return array_filter($errors);
53
    }
54
}
55